【问题标题】:error TS2304: Cannot find name 'Map' when using karma-typescript错误 TS2304:使用 karma-typescript 时找不到名称“地图”
【发布时间】:2018-11-08 05:59:32
【问题描述】:

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

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

  • 我在 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成功运行了应用程序

【问题讨论】:

  • 你试过 "target": "es2015",
  • 我现在有,不幸的是它会产生相同的编译错误。

标签: typescript karma-jasmine karma-typescript


【解决方案1】:

我在 karma.config.js 中添加了以下几行(灵感来自 this github issue

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

这消除了编译器错误。我现在遇到运行时错误,但这是另一个问题。

【讨论】:

  • 是的..实际上'Map','WeakMap'等指的是ES2015语法..我这里忘记大写了
猜你喜欢
  • 2017-06-23
  • 2023-02-15
  • 2015-09-19
  • 2016-12-18
  • 2019-05-06
  • 2019-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多