chai版本问题

环境

根目录.mocharc.json

1
2
3
4
5
6
7
8
9
10
{
"require": "ts-node/register",
"spec": "src/test/**/*.ts",
"extension": ["ts"],
"timeout": 5000,
"reporter": "spec",
"slow": 300,
"ui": "bdd",
"recursive": true
}

根目录package.json部分

1
2
3
4
5
6
7
8
9
10
11
12
{
"type": "commonjs",
"scripts": {
"test": "mocha"
},
"dependencies": {
"@types/chai": "^4.3.16",
"@types/mocha": "^10.0.6",
"chai": "^4.4.1",
"mocha": "^10.4.0"
}
}

复现

运行npm run test报错

1
Exception during run: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for ......................

解决

chai当前5.x版本仅支持纯ESM,而当前项目type为commonjs,安装char的4.x版本即可

1
2
3
npm uninstall @types/chai chai
npm cache clean --force
npm install chai@4 @types/chai@4

相关链接

v5 - Unknown file extension ".ts" when running with mocha · Issue #1568 · chaijs/chai (github.com)