angular - 为什么在 angular5 上运行 ng test 时会出现此错误?

标签 angular karma-runner karma-jasmine

我正在将我的 angular4 应用程序更新为 angular5,当我运行 ng 测试时出现此错误。我没有在angular4上得到这个,所以我觉得它与安装过程有关。任何关于为什么会发生这种情况的想法将不胜感激

...
    s/@angular/platform-browser/src/dom/dom_adapter.d.ts(80,58): error TS2304: Cannot find name 'HTMLStyleElement'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(84,44): error TS2304: Cannot find name 'Node'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(85,26): error TS2304: Cannot find name 'Node'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(85,33): error TS2304: Cannot find name 'Node'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(86,66): error TS2304: Cannot find name 'HTMLElement'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(87,64): error TS2304: Cannot find name 'HTMLElement'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(107,36): error TS2304: Cannot find name 'HTMLDocument'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(108,36): error TS2304: Cannot find name 'Document'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(110,28): error TS2304: Cannot find name 'Document'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(111,28): error TS2304: Cannot find name 'Document'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(119,34): error TS2304: Cannot find name 'Node'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(119,41): error TS2304: Cannot find name 'Node'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(120,30): error TS2304: Cannot find name 'Node'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(120,37): error TS2304: Cannot find name 'Node'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(126,40): error TS2304: Cannot find name 'Document'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(127,28): error TS2304: Cannot find name 'History'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(128,29): error TS2304: Cannot find name 'Location'.
    node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(129,31): error TS2304: Cannot find name 'Document'.
    node_modules/@angular/platform-browser/src/dom/dom_tokens.d.ts(10,47): error TS2304: Cannot find name 'Document'.
    node_modules/@angular/platform-browser/src/dom/events/dom_events.d.ts(14,31): error TS2304: Cannot find name 'HTMLElement'.
    node_modules/@angular/platform-browser/src/dom/events/event_manager.d.ts(21,31): error TS2304: Cannot find name 'HTMLElement'.
    node_modules/@angular/platform-browser/src/dom/events/event_manager.d.ts(30,40): error TS2304: Cannot find name 'HTMLElement'.
    node_modules/@angular/platform-browser/src/dom/events/hammer_gestures.d.ts(29,26): error TS2304: Cannot find name 'HTMLElement'.
    node_modules/@angular/platform-browser/src/dom/events/hammer_gestures.d.ts(35,31): error TS2304: Cannot find name 'HTMLElement'.
    node_modules/@angular/platform-browser/src/dom/events/key_events.d.ts(16,31): error TS2304: Cannot find name 'HTMLElement'.
    node_modules/@angular/platform-browser/src/dom/events/key_events.d.ts(20,35): error TS2304: Cannot find name 'KeyboardEvent'.
    node_modules/@angular/platform-browser/src/dom/shared_styles_host.d.ts(20,23): error TS2304: Cannot find name 'Node'.
    node_modules/@angular/platform-browser/src/dom/shared_styles_host.d.ts(21,26): error TS2304: Cannot find name 'Node'.

我的 ts.config 文件看起来像这样

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

我也试过安装 typings。我不断得到它。是什么原因造成的?

最佳答案

尝试如下更改 tsconfig.app.json 和 tsconfig.spec.json 文件:

tsconfig.spec.json:

{ 
    "compilerOptions": {
        "sourceMap": true,
        "declaration": false,
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "target": "es5",
        "lib": [
            "es2017",
            "dom"
        ],
        "outDir": "../out-tsc/app",
        "module": "es2015",
        "baseUrl": "",
        "types": [] 
    },
    "exclude": [
        "test.ts",
        "**/*.spec.ts"
    ]
}

tsconfig.spec.json:

{
  "compilerOptions": {
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "es2017",
      "dom"
    ],
    "outDir": "../out-tsc/spec",
    "module": "commonjs",
    "target": "es5",
    "baseUrl": "",
    "types": [
      "jasmine",
      "node"
    ]
  },
  "files": [
    "test.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}

然后最后确保您的 e2e/tsconfig.e2e.json 具有如下条目:

{
  "compilerOptions": {
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "es2017"
    ],
    "outDir": "../out-tsc/e2e",
    "module": "commonjs",
    "target": "es5",
    "types":[
      "jasmine",
      "node"
    ]
  }
}

关于angular - 为什么在 angular5 上运行 ng test 时会出现此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47417037/

相关文章:

angular - karma 测试 : Random Test fails when using async in other tests

angular - 添加到 NgModule.schemas 的 CUSTOM_ELEMENTS_SCHEMA 仍然显示错误

json - 无法升级到 Angular4

angular - Angular 中关于 routerLink 和展示组件的最佳实践是什么?

angular - routerLink 附加我的链接

javascript - 为什么我的 Angular 测试不像我的自定义表单验证功能?

node.js - 安装睾丸时出错

Angular4如何启用测试覆盖率

node.js - 通过 Angular App 和 NodeJS 发送 GET/POST 请求到 express 服务器时如何修复 404 Not Found

unit-testing - 使用 Jasmine 进行angular2测试订阅方法