[英]How to use Node js Assert with Mocha in a ES6 module project
I use Node.js v14.17.0, Mocha 9.0.2.我使用 Node.js v14.17.0,Mocha 9.0.2。 My project is a bare-bone project configured to use ES6 js module made for the understanding of this issue.我的项目是一个配置为使用 ES6 js 模块的准系统项目,用于理解这个问题。
{
"name": "mocha-tlp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"author": "",
"license": "ISC",
"type": "module"
}
With the test case ( test/foo.js
):使用测试用例( test/foo.js
):
import assert from 'assert/strict';
describe('MyObject', function () {
describe('#myFunction()', function () {
it('should work', function () {
assert(true, "All good");
});
});
});
I get the following error :我收到以下错误:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\xxxxxxxxxxxxxx\test\foo.js
require() of ES modules is not supported.
require() of C:\xxxxxxxxxxxxxx\test\foo.js from C:\xxxxxxxxxxxxxx\node_modules\mocha\lib\esm-utils.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename foo.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\projects\svelte\dgb-app\package.json.
It does not seems to be a issue that esm
and mocha --require esm
could solve because both Node.js and Mocha supports ESM for those version.这似乎不是esm
和mocha --require esm
可以解决的问题,因为 Node.js 和 Mocha 都支持这些版本的 ESM。
How can I use Nodejs Assert with Mocha when ESM project ?在 ESM 项目中如何使用 Nodejs Assert 和 Mocha?
由于 assert 是一个内置的节点模块,你可以使用:
import assert from 'node:assert/strict'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.