繁体   English   中英

ts-mocha 和 chai - 错误 TS2695:逗号运算符的左侧未使用且没有副作用

[英]ts-mocha and chai - error TS2695: Left side of comma operator is unused and has no side effects

工具/库版本:

"ts-mocha": "^8.0.0",
"ts-node": "^10.3.0",
"chai": "^4.3.4",

代码:

expect(wrapper.find(MyListItem)).to.have.length(3);

命令:

ts-mocha tests/**/*.tsx  -r unitTestSetup.ts

我的打字稿编译器选项:

{
"compilerOptions": {
  "target": "es5",
  "module": "commonjs",
  "declaration": true,
  "outDir": "./lib",
  "strict": true,
  "esModuleInterop": true,
},

错误:

tests/index.spec.tsx:41:10 - error TS2695: Left side of comma operator is unused and has no side effects.
41         (0, chai_1.expect)(wrapper.find(MyListItem)).to.have.length(3); 

这似乎是由于未安装 ts-mocha 的可选依赖项造成的。 ( tsconfig-paths )

有没有比在 ts-mocha 包上执行npm install --force来强制它安装 tsconfig-paths 更好的方法来解决这个问题? (我提供的答案)

我通过在我的package.json文件中创建一个“fix-ts-mocha”脚本来解决这个问题:

"fix-ts-mocha": "cd node_modules && cd ts-mocha && npm install --force"

我在npm install之后运行一次(或者每次更新ts-mocha时)。

我在使用 ts-node 的普通摩卡咖啡时遇到了同样的问题。 原因是 ES 模块包感染了代码。 另请参见此处

有些人尝试切换到像这里这样的 ES 模块,但这对我不起作用,因为我仍然有大部分 commonjs 模块。

我的解决方案是使用 ES 模块摆脱 npm 包(即在package.json中有"type": "module" )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM