找不到名称'DebugElement'

时间:2018-02-02 06:04:16

标签: angular karma-jasmine

使用ng test运行测试时出现此错误:

  

C:/.../ src / app / my / my.component.spec.ts(111,27)中的错误:找不到名称'DebugElement'。

在其他SO帖子(例如this one)中,推荐的解决方案是将"lib": ["dom"]添加到compilerOptionstsconfig.json文件中的tsconfig.spec.json,但对我来说该解决方案无效,我在"dom"数组中已经有"lib",您可以在此处看到:

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

tsconfig.spec.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "baseUrl": "./",
    "module": "commonjs",
    "target": "es5",
    "types": [
      "jasmine",
      "node"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  },
  "files": [
    "test.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}

发生错误的行是此函数的第一行:

export function click(el: DebugElement | HTMLElement, eventObj: any = ButtonClickEvents.left): void {
    if (el instanceof HTMLElement) {
        el.click();
    } else {
        el.triggerEventHandler('click', eventObj);
    }
}

我还能错过什么?

1 个答案:

答案 0 :(得分:3)

将其从angular/core导入为

import { DebugElement } from "@angular/core";