gpt4 book ai didi

javascript - Mocha 测试 'esm' 支持原生 ES6 模块

转载 作者:行者123 更新时间:2023-12-04 04:31:33 25 4
gpt4 key购买 nike

Alex Gibson 有一篇很棒的帖子“Testing native ES modules using Mocha and esm”。谢谢他。
我尝试在我的项目中使用 mocha 和原生 ES 模块支持,但我遇到了 2 个不同的错误:

$ ./node_modules/mocha/bin/mocha --require esm './test/Util.test.js'

TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback was not specified.
at exports.importModuleDynamicallyCallback (internal/process/esm_loader.js:41:9)
at formattedImport (/.../node_modules/mocha/lib/esm-utils.js:6:23)
at Object.exports.requireOrImport (/.../node_modules/mocha/lib/esm-utils.js:23:14)
at Object.exports.loadFilesAsync (/.../node_modules/mocha/lib/esm-utils.js:33:34)
at Mocha.loadFilesAsync (/.../node_modules/mocha/lib/mocha.js:427:19)
...
$ /usr/bin/node /.../node_modules/mocha/bin/mocha -r esm --ui bdd --reporter \ 
/.../PhpStorm/plugins/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js \
/.../test/Util.test.js

TypeError: Invalid host defined options
at formattedImport (/.../node_modules/mocha/lib/esm-utils.js:6:23)
at Object.exports.requireOrImport (/.../node_modules/mocha/lib/esm-utils.js:23:14)
at Object.exports.loadFilesAsync (/.../node_modules/mocha/lib/esm-utils.js:33:34)
at Mocha.loadFilesAsync (/.../node_modules/mocha/lib/mocha.js:427:19)
...

最佳答案

根据我在过去 2 小时中学到的知识:
包.json

{
"type": "module"
}
tsconfig.json
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "node",
}
}
.mocharc.json
{
"node-option": [
"experimental-specifier-resolution=node",
"loader=ts-node/esm"
]
}

关于javascript - Mocha 测试 'esm' 支持原生 ES6 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64261239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com