为什么在angular5上运行ng测试时会出现此错误?

为什么在angular5上运行ng测试时会出现此错误?,angular,karma-runner,karma-jasmine,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/platfor

我正在将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"
  ]
}

我也试过安装打字机。我一直都能得到。原因是什么?

这是因为您在
src
路径的根目录中定义了
tsconfig.json
(或根文件夹,如果您使用它作为基础)中的键入

编译器选项
中有一个名为
类型
的属性,它是代码使用的
类型根
中定义的环境类型数组。要包含所有已安装的类型,只需将其全部删除即可。如果只希望包含特定类型,则添加代码使用的每个已安装的环境类型

至于文档和其他浏览器类型,您应该确保
lib
设置正确,并且包含
dom

以下是一个例子:

{
  "compileOnSave": false,
  "compilerOptions": {
    "moduleResolution": "node",
    "target": "es5",
    "types": [
      "node",
      "additional ambient type roots"
    ]
    "lib": [
      "es2017",
      "dom"
    ]
  }
}
或包含所有已安装的类型

{
  "compileOnSave": false,
  "compilerOptions": {
    "moduleResolution": "node",
    "target": "es5",
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

尝试更改tsconfig.app.json和tsconfig.spec.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"
    ]
  }
}
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"
    ]
}
{
  "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"
  ]
}
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"
    ]
}
{
  "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"
    ]
  }
}

您使用的是什么版本的typeScript?@Taki我使用的是typeScript 2.4.2我尝试了您的建议,但没有效果。@Dan-确保您指向的是正确的
tsconfig.json
文件。您可以在
.angular cli.json
文件中对此进行验证。我想你正在使用cli?这对我很有效。我在tsconfig.json中添加了“dom”,但不知道tsconfig.spec.json。我能够很好地构建项目(因为它在tsconfig中是正确的),但在测试时总是有问题。需要确保“dom”位于tsconfig.spec.json文件的lib数组中。