TypeScript编译中缺少错误home.component.spec.ts请通过“文件”或“包含”属性确保它位于您的tsconfig中

问题描述

在运行ng测试时,我收到以下错误home.component.spec.ts从TypeScript编译中丢失。请通过“文件”或“包含”属性确保它在您的tsconfig中。

项目结构

Project/
|-- src/
|   |-- app/
|   |   |-- home/
|   |   |   |-- home.component.spec.ts  
|   |   |-- app-main.component.spec.ts
|   |-- test.ts
|   |-- tsconfig.spec.json
|-- angular.json
|-- package.json
|-- tsconfig.json

tsconfig.spec.json

{
  "extends": "../tsconfig.json","compilerOptions": {
    "outDir": "../out-tsc/spec","types": [
      "jasmine","node"
    ]
  },"files": [
    "test.ts","polyfills.ts","app/app-main.component.spec.ts" 
    // explicitly targetting spec.ts file works,// but I want to add them to the include property
    // "app/home/home.component.spec.ts" // this fixes it ...
  ],"include": [
    "**/*.spec.ts",// can't find app/app-main.component.spec.ts
    "app/*.spec.ts",// can't find app/app-main.component.spec.ts
    "**/**/*.spec.ts",// can't find app/home/home.component.spec.ts
    "app/home/*.spec.ts",// can't find app/home/home.component.spec.ts
    "**/*.d.ts"
  ]
}

仅当我明确添加规格文件时,我想将这些文件/目录添加到include块中,这样我才不需要一一明确添加规格文件

但是无论我尝试在include部分添加什么内容,都行不通。

解决方法

您必须将tsconfig.json中的路径重新注册到tsconfig.spec.json文件中。

例如,如果您的tsconfig.json文件中包含以下内容

{
...
"paths": {
   "@/*": [
   "./*","app/*","src/app/*","../node_modules/*"
  ]
}
...
}

这也必须位于您的tsconfig.spec.json文件中。