【问题标题】:Mocha typescript compilation issue related to downlevelIteration与 downlevelIteration 相关的 Mocha typescript 编译问题
【发布时间】:2020-05-18 03:53:47
【问题描述】:

我刚刚尝试将 Mocha 整合到我的 Typescript 应用程序中。这是我的 tsconfig.json

{
         "compilerOptions": {
             "target": "ES6",
             "module": "commonjs",
             "outDir": "../dist",
             "sourceMap": true,
             "esModuleInterop": true,
             "downlevelIteration": true
         },
         "include": [
             "**/*.ts"
         ]
}

这是我的 package.json

{
  "name": "game_server",
  "version": "1.0.0",
  "description": "A backend server for the game ",
  "scripts": {
    "test": "mocha -r ts-node/register src/**/*.spec.ts",
    "start": "concurrently \"tsc -p ./src -w\" \"nodemon ./dist/app.js\" "
  },
  "author": "XXXXX",
  "dependencies": {
    "@types/express": "^4.17.6",
    "@types/shortid": "0.0.29",
    "@types/socket.io": "^2.1.4",
    "express": "^4.17.1",
    "shortid": "^2.2.15",
    "socket.io": "^2.3.0",
    "typedeck": "^1.5.2"
  },
  "devDependencies": {
    "@types/chai": "^4.2.11",
    "@types/mocha": "^7.0.2",
    "chai": "^4.2.0",
    "concurrently": "^5.2.0",
    "mocha": "^7.1.2",
    "nodemon": "^2.0.3",
    "ts-node": "^8.10.1",
    "typescript": "^3.9.2"
  }
}

在我的代码中的某个时刻,我使用 for .. of 迭代器来生成一个看起来像的地图

for (let [key, value] of cardMap) {
}

当我执行npm run start 时,我的代码编译良好并且服务器启动

但是当我做 npm run test 时,它会扩展为

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

我在上面的行中遇到错误

TSError: ⨯ Unable to compile TypeScript:
src/state/GameState.ts:183:30 - error TS2569: Type 'Map<Player, Card[]>' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.

183     for (let [key, value] of cardMap) {
                                 ~~~~~~~

我尝试在我的 tsconfig 中加入 downLevelIteration=true,我是否遗漏了一些东西来让 mocha 测试正常工作?

【问题讨论】:

    标签: typescript mocha.js tsc


    【解决方案1】:

    我开始使用 ts-mocha 并将我的测试脚本更改为 ts-mocha -p ./src/tsconfig.json ./src/**/*.spec.ts 并且它开始工作

    https://github.com/piotrwitek/ts-mocha#readme

    【讨论】:

      猜你喜欢
      • 2011-09-15
      • 1970-01-01
      • 2019-12-08
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多