NodeJS 使用打字稿TS2451的Mocha变量:无法重新声明块作用域变量

vaj7vani  于 2022-11-03  发布在  Node.js
关注(0)|答案(1)|浏览(167)

我在Typescript中为Mocha创建了两个测试文件,但当尝试运行它时

mocha -r ts-node/register ./test/*.spec.ts

显示此错误

error TS2451: Cannot redeclare block-scoped variable 'assert'.

文件测试A(a.spec.ts)包含与文件测试B(b.spec.ts)示例相同的声明

const assert = require('node:assert')

这是我的打印脚本配置

{
  "compilerOptions": {
    "target": "es2016",                                
    "module": "commonjs",                           
    "declaration": true,                             
    "declarationMap": true,                        
    "outDir": "./dist",                                 
    "esModuleInterop": true,                          
    "forceConsistentCasingInFileNames": true,         
    "strict": true,                                      
    "skipLibCheck": true                               
  },
  "include": ["src/**/*", "tests/**/*", "src/lib/*"]
}

这是设备依赖关系

- "@types/mocha": "^9.1.1"  
 - "@types/node": "^17.0.35"
 - "@typescript-eslint/eslint-plugin": "^5.26.0"
 - "@typescript-eslint/parser": "^5.26.0"
 -  "eslint": "^8.16.0"
 -  "eslint-config-standard": "^17.0.0"
 -  "eslint-plugin-import": "^2.26.0"
 -  "eslint-plugin-n": "^15.2.0"
 -  "eslint-plugin-promise": "^6.0.0"
 -  "mocha": "^10.0.0"
 -  "ts-node": "^10.8.0"
 -   "typescript": "^4.7.2"

是否可能用mocha创建不同的文件测试但声明相同?

y4ekin9u

y4ekin9u1#

替换:

const assert = require('node:assert')

import assert from 'node:assert'

相关问题