faq
标签交叉引用node node_modules/.bin/mocha --version
(本地)和mocha --version
(全球)。 我们建议您_不要_全局安装 Mocha。当运行--watch
具有封装内"type": "module"
在其设置package.json
,摩卡未能正确import
,而不是尝试require()
的文件。
$ npm run test-mocha:watch
> @ test-mocha:watch /home/jdalrymple/src/bugs/mochajs
> mocha --require @babel/register $(find ./src -type f -name '*.test.js') --watch
internal/modules/cjs/loader.js:1217
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /.../src/index.test.js
require() of ES modules is not supported.
require() of /.../src/index.test.js from /.../node_modules/mocha/lib/mocha.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 index.test.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /.../package.json.
at Module._extensions..js (internal/modules/cjs/loader.js:1217:13)
at Object.newLoader [as .js] (/home/jdalrymple/src/bugs/mochajs/node_modules/pirates/lib/index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:1050:32)
at Function.Module._load (internal/modules/cjs/loader.js:938:14)
at Module.require (internal/modules/cjs/loader.js:1090:19)
at require (internal/modules/cjs/helpers.js:75:18)
at /home/jdalrymple/src/bugs/mochajs/node_modules/mocha/lib/mocha.js:384:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/home/jdalrymple/src/bugs/mochajs/node_modules/mocha/lib/mocha.js:381:14)
at Mocha.run (/home/jdalrymple/src/bugs/mochajs/node_modules/mocha/lib/mocha.js:954:10)
at Object.run (/home/jdalrymple/src/bugs/mochajs/node_modules/mocha/lib/cli/watch-run.js:223:20)
at FSWatcher.<anonymous> (/home/jdalrymple/src/bugs/mochajs/node_modules/mocha/lib/cli/watch-run.js:169:14)
at FSWatcher.emit (events.js:314:20)
at /home/jdalrymple/src/bugs/mochajs/node_modules/mocha/node_modules/chokidar/index.js:364:35
at processTicksAndRejections (internal/process/task_queues.js:75:11) {
code: 'ERR_REQUIRE_ESM'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ test-mocha:watch: `mocha --require @babel/register $(find ./src -type f -name '*.test.js') --watch`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @ test-mocha:watch script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/jdalrymple/.npm/_logs/2020-07-14T00_20_36_063Z-debug.log
为方便起见,创建了以下存储库以充分重现该问题:
npm i
npm run test-mocha
npm run test-mocha:watch
预期行为:
实际行为:
我的理解是以下场景是适用的:
import
应在以下条件下使用:
package.json
文件包含"type": "module"
并且其文件扩展名为.js
.mjs
,则不管其关联的package.json
文件中的内容require()
应在以下条件下使用:
package.json
文件不包含"type": "module"
且其文件扩展名为.js
.cjs
,则不管其关联的package.json
文件中的内容目前,似乎--watch
假设目标文件是 CommonJS 模块,而其扩展名是.js
,并且未能屈服于其package.json
文件的"type": "module"
.
值得考虑的事实是,与正在导入的文件关联的package.json
文件可能与调用 mocha 的目录中的package.json
不同,即使目标文件的路径确实如此不包括node_modules
。
请参阅我们的文档:当前限制。
我们(还?)不支持watch
模式下的 ESModules。 Node 的 ESM 实现正在使用其单独的缓存(不是require.cache
),当前无法在以watch
模式重新运行测试之前清除缓存。
@Swivelgames您是否尝试过将--parallel
与--watch
结合使用? 它可能会起作用,因为我们在每次运行后都会转储工作人员,但我认为我们还没有在任何地方对其进行测试。
@boneskull我不是@Swivelgames,但您添加 --parallel 的建议对我有用。 谢谢!
最有用的评论
@Swivelgames您是否尝试过将
--parallel
与--watch
结合使用? 它可能会起作用,因为我们在每次运行后都会转储工作人员,但我认为我们还没有在任何地方对其进行测试。