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

问题描述 投票:0回答:1

我安装了IDEA 2020.1.2 Ultimate Edition,安装了NodeJS插件,该插件是使用IDE运行Mocha测试所必需的。

当我使用IDEA(而不是控制台中的npm test来运行此简单测试时]

import assert from "assert";

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

它给我以下错误:

/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

[我知道我必须使用Babel,并且可以通过具有ES6功能的控制台对其进行设置和运行Mocha,但是如何告诉IDEA使用ES6功能?

javascript intellij-idea mocha babeljs es6-modules
1个回答
0
投票

我要做的只是:

  1. npm install --save-dev babel-register
  2. --require babel-core/register(此选项不赞成--compilers选项,请参阅https://github.com/mochajs/mocha/wiki/compilers-deprecation)。要使其永久,请编辑Mocha模板以进行运行配置:
  • edit Mocha template for run configuration
  • © www.soinside.com 2019 - 2024. All rights reserved.