error TS2304: Cannot find name 'Map' when using karma-typescript

2.4k views Asked by At

I try to run my first spec.ts file with karma-typescript and jasmine.

I already had a lot of different errors that I googled and "fixed" but I'm always getting just other problems.

  • I added different entries in tsconfig.json->compilerOptions->"lib"
  • tried to change the target
  • tried out @types/core-js with version 0.9.46 and with 0.9.35
  • updated node from 8.9.4 to 8.11.2
  • tried to downgrade @types/node to some 8.x version

There is no problem to run the app. Just the test does not compile.

Do I need to configure the compiler of karma differently?

At the moment I just use the "minimal karma configuration" from 1.

module.exports = function(config) {
config.set({
    frameworks: ["jasmine", "karma-typescript"],
    files: [
        "src/**/*.ts" // *.tsx for React Jsx
    ],
    preprocessors: {
        "**/*.ts": "karma-typescript" // *.tsx for React Jsx
    },
    reporters: ["progress", "karma-typescript"],
    browsers: ["Chrome"]
  });
};

The current error is

> ts-node node_modules/karma/bin/karma start ./karma.conf.js

29 05 2018 15:49:04.611:INFO [compiler.karma-typescript]: Compiling project using Typescript 2.8.3
29 05 2018 15:49:07.176:ERROR [compiler.karma-typescript]: node_modules/@types/node/index.d.ts(6208,55): error TS2304: Cannot find name 'Map'.

29 05 2018 15:49:07.177:ERROR [compiler.karma-typescript]: node_modules/@types/node/index.d.ts(6215,55): error TS2304: Cannot find name 'Set'.

29 05 2018 15:49:07.178:ERROR [compiler.karma-typescript]: node_modules/@types/node/index.d.ts(6219,64): error TS2304: Cannot find name 'Symbol'.

29 05 2018 15:49:07.178:ERROR [compiler.karma-typescript]: node_modules/@types/node/index.d.ts(6225,59): error TS2304: Cannot find name 'WeakMap'.

29 05 2018 15:49:07.178:ERROR [compiler.karma-typescript]: node_modules/@types/node/index.d.ts(6226,59): error TS2304: Cannot find name 'WeakSet'.

29 05 2018 15:49:07.179:ERROR [compiler.karma-typescript]: src/commands/persistence/eventStore/inmemory/InMemoryES.ts(14,26): error TS2304: Cannot find name 'Map'.

29 05 2018 15:49:07.257:INFO [compiler.karma-typescript]: Compiled 9 files in 2632 ms.
29 05 2018 15:49:07.287:INFO [karma]: Karma v2.0.2 server started at http://0.0.0.0:9876/
29 05 2018 15:49:07.288:INFO [launcher]: Launching browser Chrome with unlimited concurrency
29 05 2018 15:49:07.334:INFO [launcher]: Starting browser Chrome
29 05 2018 15:49:09.701:INFO [Chrome 66.0.3359 (Windows 10 0.0.0)]: Connected on socket JvL2wqL-IoqPWKS8AAAA with id 62494987
Chrome 66.0.3359 (Windows 10 0.0.0) ERROR
  {
    "message": "You need to include some adapter that implements __karma__.start method!",
    "str": "You need to include some adapter that implements __karma__.start method!"
  }


Unhandled rejection Error: COMPILATION ERROR
    at maybeWrapAsError (E:\_d_a_t_e_n\programmieren\private\_web\ADB\ADB-Service\node_modules\bluebird\js\release\util.js:61:12)
    at E:\_d_a_t_e_n\programmieren\private\_web\ADB\ADB-Service\node_modules\bluebird\js\release\nodeback.js:38:50

My current tsconfig.json file

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["es5", "es6", "dom"], // added because of karma problems, did not help though (https://stackoverflow.com/a/39418293/7869582, https://stackoverflow.com/questions/42738623/cannot-find-name-propertykey)
    "module": "commonjs",
    "noImplicitAny": true,
    "removeComments": true,
    "outDir": "./dist/",
    "sourceMap": true,
    "pretty": true
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "typeRoots": [
    "node_modules/@types"
  ]
}

And the dependencies from package.json

{
  "name": "adb-service",
  "version": "0.0.0",
  "private": true,
  "description": "Backend Express Server",
  "main": "node dist/index.js",
  "scripts": {
    "start": "nodemon dist/index.js",
    "dev": "concurrently --kill-others \"tsc -w\" \"nodemon dist/index.js\"",
    "test2": "ts-node node_modules/jasmine/bin/jasmine",
    "test": "ts-node node_modules/karma/bin/karma start ./karma.conf.js"
  },
  "author": "Andymel",
  "dependencies": {
    "body-parser": "^1.18.2",
    "express": "^4.16.3",
    "mongoose": "^5.1.0"
  },
  "devDependencies": {
    "@types/body-parser": "^1.16.8",
    "@types/express": "^4.11.1",
    "@types/jasmine": "^2.8.7",
    "@types/mongodb": "^3.0.13",
    "@types/mongoose": "^5.0.13",
    "@types/node": "^10.1.3",
    "concurrently": "^3.5.1",
    "jasmine": "^3.1.0",
    "karma": "^2.0.2",
    "karma-chrome-launcher": "^2.2.0",
    "karma-jasmine": "^1.1.2",
    "karma-typescript": "^3.0.12",
    "nodemon": "^1.17.3",
    "ts-node": "^6.0.5",
    "typescript": "^2.8.3"
  }
}

Thanks in advance for any tip!

I try to start the test with npm test.
I successfully run the app with npm run dev

1

There are 1 answers

1
andymel On BEST ANSWER

I added the following lines to karma.config.js (inspired by this github issue)

karmaTypescriptConfig: {
    compilerOptions: {
        target: "ES2015",
        lib: ["es5", "es6", "es2015", "dom"]
    },
    tsconfig: "tsconfig.json"
},

This removes the compiler errors. I get runtime errors now, but that's another issue.