Get the App
SLTechnology News&Howtos  ›  Development  › 

How to make mocha support ES6 module

Shulou Source: shulou.com Published: 2022-06-01 03:29:59 11月12日 Update

Today, I would like to share with you how to make mocha support ES6 module related knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.

Mocha is a commonly used node testing framework, but it only supports commonjs modules. To make mocha support ES6 modules, you need the help of babel.

The version of the tool used in writing this article is:

Babel v7mocha v6.2

Installation dependency

$npm I-D @ babel/cli @ babel/core @ babel/preset-env @ babel/register

Babel configuration

Add a configuration in package.json or .babelrc:

{"presets": ["@ babel/preset-env"]} / / "babel": {/ / "presets": [/ / "@ babel/preset-env" /] / /}

Configuration command

Finally, configure the run command. Babel/register will be bound to the require module of node, and translation will be implemented when the code is running, so that the module syntax of ES6 can be supported:

"scripts": {"test": "mocha-require @ babel/register test/*.js",}

These are all the contents of the article "how to make mocha support ES6 module". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Module support knowledge article configuration content command run different big code most that is tools common more framework version knowledge point industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info OPPO Reno Linux vpn