错误TS2304:使用karma-typescript时找不到名称'Map'

时间:2018-05-29 14:21:08

标签: typescript karma-jasmine karma-typescript

我尝试使用karma-typescript和jasmine运行我的第一个spec.ts文件。

我已经有很多不同的错误,我用Google搜索并“修复”但我总是遇到其他问题。

  • 我在tsconfig.json-> compilerOptions->“lib”中添加了不同的条目
  • 试图更改目标
  • 试用版本0.9.46和0.9.35
  • 的@ types / core-js
  • 将节点从8.9.4更新为8.11.2
  • 尝试将@ types / node降级到某个8.x版本

运行应用程序没有问题。只是测试没有编译。

我是否需要以不同的方式配置karma的编译器?

目前我只使用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"]
  });
};

当前错误

> 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

我当前的 tsconfig.json 文件

{
  "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"
  ]
}

来自 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"
  }
}

提前感谢任何提示!

我尝试用npm test开始测试 我使用npm run dev

成功运行了该应用

1 个答案:

答案 0 :(得分:1)

我将以下几行添加到karma.config.js(受this github issue启发)

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

这将删除编译器错误。我现在得到运行时错误,但这是另一个问题。