Angular VSCode 无法识别单元测试的类型定义

Angular Type Definitions for Unit Tests Not Recognized by VSCode

问题

我有一个 Angular 9 应用程序,刚刚向组件添加了一个小单元测试。但是 VSCode 无法识别 describebeforeEachit 等类型。尽管我有一个 tsconfig.spec.json 文件,其 types 定义中包含 jasmine

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

我得到的实际错误如下:

Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.ts(2582)

我尝试过的方法(但没有用)

  1. 通过 yarn add -D @types/jasmine
  2. 重新安装 @types/jasmine
  3. jasmine 添加到 tsconfig.jsontypes
  4. tsconfig.spec.json
  5. 中删除 types 定义
  6. 重新安装我项目的所有依赖项
  7. 在我的 monorepo
  8. 的根目录中将 jasmine 添加到 tsconfig.jsontypes

自己试试

如果你想仔细看看你可以通过

自己尝试一下
git clone https://github.com/flolu/cents-ideas
git checkout 45bda5235f832ab801d6439d0179dd6c0e76c4cc

那么/services/client/src/app/hello-world/hello-world.component.spec.ts就是有错误的文件

旁注:测试通过,所以只是VSCode找不到类型的问题。

我的 tsconfig.spec.json 还好。只需从

修复我的 tsconfig.json 中的 typeRoots 路径
"typeRoots": ["node_modules/@types"]

"typeRoots": ["../../node_modules/@types"]

因为在我的 monorepo 中,我只有项目根目录中的 node_modules 文件夹。

请尝试导入

  import {} from 'jasmine';