简体   繁体   English

如何在 IntelliJ IDEA 的 Mocha 测试中使用 ES6 导入?

[英]How to use ES6 imports in Mocha tests in IntelliJ IDEA?

I have IDEA 2020.1.2 Ultimate Edition, I have NodeJS plugin installed that is required for running Mocha tests with IDE.我有 IDEA 2020.1.2 Ultimate Edition,我安装了使用 IDE 运行 Mocha 测试所需的 NodeJS 插件。

When I run this simple test with IDEA (not with npm test in console)当我使用 IDEA 运行这个简单的测试时(不是在控制台中使用npm test

import assert from "assert";

it('must return true', () => {
    assert.equal(true, true);
});

it gives me the following error:它给了我以下错误:

/usr/bin/node /home/chriego/Projects/personal/my-project/frontend/node_modules/mocha/bin/mocha --ui bdd --reporter /opt/idea/plugins/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js /home/chriego/Projects/personal/my-project/frontend/tests/time.js --grep "^должно возвращать true$"

/home/chriego/Projects/personal/my-project/frontend/tests/time.js:1
import assert from "assert";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1116:16)
    at Module._compile (internal/modules/cjs/loader.js:1164:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Module.require (internal/modules/cjs/loader.js:1089:19)
    at require (internal/modules/cjs/helpers.js:73:18)
    at Object.exports.requireOrImport (/home/chriego/Projects/personal/my-project/frontend/node_modules/mocha/lib/esm-utils.js:20:12)
    at Object.exports.loadFilesAsync (/home/chriego/Projects/personal/my-project/frontend/node_modules/mocha/lib/esm-utils.js:33:34)
    at Mocha.loadFilesAsync (/home/chriego/Projects/personal/my-project/frontend/node_modules/mocha/lib/mocha.js:421:19)
    at singleRun (/home/chriego/Projects/personal/my-project/frontend/node_modules/mocha/lib/cli/run-helpers.js:156:15)
    at exports.runMocha (/home/chriego/Projects/personal/my-project/frontend/node_modules/mocha/lib/cli/run-helpers.js:225:10)
    at Object.exports.handler (/home/chriego/Projects/personal/my-project/frontend/node_modules/mocha/lib/cli/run.js:366:11)
    at /home/chriego/Projects/personal/my-project/frontend/node_modules/mocha/node_modules/yargs/lib/command.js:241:49

Process finished with exit code 1

I know that I have to use Babel, and I can set it up and run Mocha from console with ES6 features, but how do I tell IDEA to use ES6 features?我知道我必须使用 Babel,我可以使用 ES6 功能从控制台设置并运行 Mocha,但是我如何告诉 IDEA 使用 ES6 功能?

All I had to do was:我所要做的就是:

  1. npm install --save-dev babel-register
  2. Add --require babel-core/register in Mocha test run configuration settings under "Extra Mocha options" (this deprecates --compilers option, see https://github.com/mochajs/mocha/wiki/compilers-deprecation ).在“Extra Mocha 选项”下的 Mocha 测试运行配置设置中添加--require babel-core/register (这不推荐使用--compilers选项,请参阅https://github.com/mochajs/mocha/wiki/compilers-deprecation )。 To make it permanent, edit Mocha template for run configuration:要使其永久化,请编辑 Mocha 模板以进行运行配置:

编辑 Mocha 模板以进行运行配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM