[英]How to use Node js Assert with Mocha in a ES6 module project
我使用 Node.js v14.17.0,Mocha 9.0.2。 我的項目是一個配置為使用 ES6 js 模塊的准系統項目,用於理解這個問題。
{
"name": "mocha-tlp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"author": "",
"license": "ISC",
"type": "module"
}
使用測試用例( test/foo.js
):
import assert from 'assert/strict';
describe('MyObject', function () {
describe('#myFunction()', function () {
it('should work', function () {
assert(true, "All good");
});
});
});
我收到以下錯誤:
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.
這似乎不是esm
和mocha --require esm
可以解決的問題,因為 Node.js 和 Mocha 都支持這些版本的 ESM。
在 ESM 項目中如何使用 Nodejs Assert 和 Mocha?
由於 assert 是一個內置的節點模塊,你可以使用:
import assert from 'node:assert/strict'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.