Search code examples
typescriptmocha.jstsc

Mocha typescript compilation issue related to downlevelIteration


I just tried incorporating Mocha into my Typescript application. This is my tsconfig.json

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

and this is my 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"
  }
}

At some point in my code I am using an for .. of iterator for a map that looks like

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

When I do a npm run start my code gets compiled fine and the server starts

but when I do npm run test which expands to

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

I get an error on the above line

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) {
                                 ~~~~~~~

I have tried incorporating downLevelIteration=true in my tsconfig, am I missing something to get mocha tests working?


Solution

  • I started using ts-mocha and changed my test script to ts-mocha -p ./src/tsconfig.json ./src/**/*.spec.ts and it started working

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