问题描述
我正在尝试使用 mocha 做一个非常简单的测试(没有配置文件,没有额外的标志,只有 mocha
、yarn2
和 testee.js
文件),但它总是给出我0 passing
。该死,它甚至不会运行任何文件!
// testee.js
console.log('test') // No output
describe('something',() => {
it('Should run',() => {
console.log('test 2') // No output either
})
})
$ yarn mocha testee.js
0 passing (1ms)
我正在使用的工具:
- 摩卡 9.0.2
- Yarn Berry 2.4.2
Yarn 2 不支持 mocha 吗?我应该使用其他东西吗?我总是将 mocha 用于我的所有测试文件,如果确实如此,也许是时候进行迁移了。
注意:我尝试使用 yarn 1 并且它完美无缺。另外,Mocha 找到了 testee.js
文件,否则它会给我 not found
错误而不是 0 passing
解决方法
Mocha 9 使用 ESM-first 方法导入 https://github.com/mochajs/mocha/releases/tag/v9.0.0
采用默认 PnP 安装方案的 Yarn 2+ 尚不支持 ESM,因为 Node API 缺少一些功能来实现这一点
目前,如果您想使用 Mocha 9,您必须将 node_modules
安装方案与 Yarn 2+ 一起使用,方法是将您的配置更改为:
.yarnrc.yml
nodeLinker: node-modules
...
并运行 yarn
以使用 node_modules
您可以在此处跟踪 Yarn PnP 的 ESM 支持: https://github.com/yarnpkg/berry/issues/638