ng测试在角度通用错误中失败“错误:未找到规格,随机种子48751”

时间:2019-09-17 09:31:09

标签: angular angular-universal angular-testing angular-unit-test

在运行“ ng test”命令时将angular 7投影转换为通用角度,错误为“ Incomplete: No specs found, , randomized with seed 48751”。 尝试过不同的方法来提及stackoverflow,但对我没有任何帮助。

ERROR in ./src/polyfills.ts
Module build failed (from ./node_modules/@ngtools/webpack/src/index.js):
Error: ../src/polyfills.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
    at AngularCompilerPlugin.getCompiledFile (../node_modules/@ngtools/webpack/src/packages/ngtools/webpack/src/angular_compiler_plugin.ts:1024:15)
    at plugin.done.then (../node_modules/@ngtools/webpack/src/packages/ngtools/webpack/src/loader.ts:49:29)
    at process._tickCallback (internal/process/next_tick.js:68:7)
 @ multi ./src/polyfills.ts ./node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/jit-polyfills.js polyfills[0]

要在ng测试命令中正常运行的预期输出不会出现任何问题,以便我的单元测试用例能够执行。

5 个答案:

答案 0 :(得分:1)

最后,经过大量实验,找到了解决方案。 只需添加

`"include": [
    "**/*.spec.ts",
    "**/*.d.ts",
    "**/*.ts"
  ]`

在“ tsconfig.spec.json”中,希望对您有所帮助:)

答案 1 :(得分:1)

您只需要具有正确的路径即可。

tsconfig.spec.json文件中的以下内容有很多错误:

          "files": [
            "test.ts",
            "polyfills.ts"
          ],
          "include": [
            "**/*.spec.ts",
            "**/*.d.ts"
          ]

然后将路径更改为以下内容后,一切正确:

          "files": [
            "src/test.ts",
            "src/polyfills.ts"
          ],
          "include": [
            "src/**/*.spec.ts",
            "src/**/*.d.ts"
          ]

答案 2 :(得分:0)

对我来说,实际上是导致它的编译错误。我也收到了:

error TS2591: Cannot find name 'Buffer'. Do you need to install type definitions for node? Try `npm i @types/node` and then add `node` to the types field in your tsconfig.

通过在"types": ["node"]的editorOptions中添加 "typeRoots": ["node_modules/@types"]tsconfig.spec.json来解决此问题,最终使其看起来像这样:

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "outDir": "./out-tsc/spec",
    "types": [
      "jasmine",
      "node"
    ],
    "typeRoots": ["node_modules/@types"]
  },
  "files": [
    "src/test.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.spec.ts",
    "src/**/*.d.ts"
  ]
}

答案 3 :(得分:0)

检查您的 angular.json 文件。您可能有错误的配置。脚本文件在样式中或样式在脚本中。

我在 style 标签中包含了几个 .js 文件,导致了这个问题。

答案 4 :(得分:0)

如果某些单元测试文件中存在一些编译错误,则可能会发生此错误,如果发生这种情况,则会在运行“ng test”的命令提示符中看到编译错误。

例如,内置的“app.component.spec.ts”包含一个测试,期望 title 变量具有 App 标题。如果你已经从“app.component.ts”中删除了这个变量,测试仍然包含它的事实不会导致构建失败,但在运行测试时会发生错误。