dri*_*ood 25 javascript intellij-idea mocha.js chai
我正在尝试在 intellij 中编写一个 javascript 测试,我需要为其导入一些依赖项,并且我想使用 ES6 样式的导入语句,但出现错误
/usr/local/bin/node /workspace/rr-sample/node_modules/mocha/bin/_mocha
--ui bdd --reporter "/Users/me/Library/Application Support/IntelliJIdea2019.1/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js"
tests/*.test.js /workspace/rr-sample/tests/App.test.js:3
import chai from 'chai'
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1043:16)
at Module._compile (internal/modules/cjs/loader.js:1091:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
at Module.load (internal/modules/cjs/loader.js:976:32)
at Function.Module._load (internal/modules/cjs/loader.js:884:14)
at Module.require (internal/modules/cjs/loader.js:1016:19)
at require (internal/modules/cjs/helpers.js:69:18)
at /workspace/rr-sample/node_modules/mocha/lib/mocha.js:334:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/workspace/rr-sample/node_modules/mocha/lib/mocha.js:331:14)
at Mocha.run (/workspace/rr-sample/node_modules/mocha/lib/mocha.js:809:10)
at Object.exports.singleRun (/workspace/rr-sample/node_modules/mocha/lib/cli/run-helpers.js:108:16)
at exports.runMocha (/workspace/rr-sample/node_modules/mocha/lib/cli/run-helpers.js:142:13)
at Object.exports.handler (/workspace/rr-sample/node_modules/mocha/lib/cli/run.js:292:3)
at Object.runCommand (/workspace/rr-sample/node_modules/yargs/lib/command.js:242:26)
at Object.parseArgs [as _parseArgs] (/workspace/rr-sample/node_modules/yargs/yargs.js:1087:28)
at Object.parse (/workspace/rr-sample/node_modules/yargs/yargs.js:566:25)
at Object.exports.main (/workspace/rr-sample/node_modules/mocha/lib/cli/cli.js:68:6)
at Object.<anonymous> (/workspace/rr-sample/node_modules/mocha/bin/_mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:1121:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
at Module.load (internal/modules/cjs/loader.js:976:32)
at Function.Module._load (internal/modules/cjs/loader.js:884:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:67:12)
at internal/main/run_main_module.js:17:47
Run Code Online (Sandbox Code Playgroud)
究竟是什么问题?我发现这个链接(和其他人)http://xahlee.info/js/js_import_export.html它告诉你如何解决这个错误,但在另一个对我没有帮助的上下文中,它没有解释问题是什么.
如果有帮助,这里是我正在使用的代码。
//const chai = require("chai");
import chai from 'chai'
const React = require("react");
const expect = chai.expect;
describe('how it works first-time test', () => {
it('checks equality', () => {
const val = false;
expect(val).to.be.false;
});
});
Run Code Online (Sandbox Code Playgroud)
len*_*ena 18
运行用 ES6 编写的 Mocha 测试的最简单方法是使用 Mocha--require @babel/register选项即时编译它们(参见https://github.com/mochajs/mocha/wiki/compilers-deprecation#what-should-i-use-instead -然后)。当然,你需要确保安装相应的模块,并设置了.babelrc相应的
包.json:
"dependencies": {
"@babel/cli": "^7.7.4",
"@babel/core": "^7.7.4",
"@babel/preset-env": "^7.7.4",
"@babel/register": "^7.7.4",
...
}
Run Code Online (Sandbox Code Playgroud)
.babelrc:
{
"presets": [
[
"@babel/preset-env"
]
]
}
Run Code Online (Sandbox Code Playgroud)
另见https://dev.to/bnorbertjs/my-nodejs-setup-mocha--chai-babel7-es6-43ei
and*_*mel 14
将我的 ts 库之一更新为 es6 模块而不是 commonjs 时,我遇到了同样的问题。在 tsconfig.json 更改后,我npm run test产生了上述错误。
import chai from 'chai'
^^^^^^
SyntaxError: Cannot use import statement outside a module
Run Code Online (Sandbox Code Playgroud)
我通过添加一个自己的 tsconfig 文件仅用于测试,在没有 babel的情况下解决了它。
tsconfig.testing.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es6"
},
"include": ["**/*.spec.ts"]
}
Run Code Online (Sandbox Code Playgroud)
通过 package.json 中的脚本运行测试
"test": "cross-env TS_NODE_PROJECT=\"tsconfig.testing.json\" mocha -r ts-node/register src/**/*.spec.ts",
Run Code Online (Sandbox Code Playgroud)
(cross-env 设置 env 变量 os 独立)
我可以通过在项目源中添加 .mocharc.yaml 文件来实现此功能,其中包含以下内容:
require: '@babel/register'
Run Code Online (Sandbox Code Playgroud)
来源: https: //github.com/mochajs/mocha-examples/tree/master/packages/babel
小智 0
根据文档:
https://nodejs.org/api/esm.html#esm_code_import_code_statements
所以你必须确保将脚本作为 es 模块执行。
例如,使用babel-node而不是 Nodejs执行脚本以启用 es6 或更高版本。
| 归档时间: |
|
| 查看次数: |
40984 次 |
| 最近记录: |