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

flo*_*olu 5 unit-testing jasmine typescript visual-studio-code angular

问题

我有一个 Angular 9 应用程序,刚刚向组件添加了一个小型单元测试。describe但 VSCode 无法识别, beforeEach,等类型it。虽然我有一个tsconfig.spec.json文件,jasminetypes定义如下:

{
  "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"]
}
Run Code Online (Sandbox Code Playgroud)

我得到的实际错误如下:

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)
Run Code Online (Sandbox Code Playgroud)

未找到类型

我尝试过的(但没有成功)

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

自己尝试一下

如果你想仔细看看,你可以自己尝试一下

git clone https://github.com/flolu/cents-ideas
git checkout 45bda5235f832ab801d6439d0179dd6c0e76c4cc
Run Code Online (Sandbox Code Playgroud)

然后/services/client/src/app/hello-world/hello-world.component.spec.ts是有错误的文件

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

flo*_*olu 3

我的 tsconfig.spec.json 没问题。只需修复typeRoots我的 tsconfig.json 中的路径即可

"typeRoots": ["node_modules/@types"]
Run Code Online (Sandbox Code Playgroud)

"typeRoots": ["../../node_modules/@types"]
Run Code Online (Sandbox Code Playgroud)

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