Angular VSCode 无法识别单元测试的类型定义
Angular Type Definitions for Unit Tests Not Recognized by VSCode
问题
我有一个 Angular 9 应用程序,刚刚向组件添加了一个小单元测试。但是 VSCode 无法识别 describe
、beforeEach
、it
等类型。尽管我有一个 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)
我尝试过的方法(但没有用)
- 通过
yarn add -D @types/jasmine
重新安装 @types/jasmine
- 将
jasmine
添加到 tsconfig.json 的 types
- 从 tsconfig.spec.json
中删除 types
定义
- 重新安装我项目的所有依赖项
- 在我的 monorepo
的根目录中将 jasmine
添加到 tsconfig.json 的 types
自己试试
如果你想仔细看看你可以通过
自己尝试一下
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';
问题
我有一个 Angular 9 应用程序,刚刚向组件添加了一个小单元测试。但是 VSCode 无法识别 describe
、beforeEach
、it
等类型。尽管我有一个 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)
我尝试过的方法(但没有用)
- 通过
yarn add -D @types/jasmine
重新安装 - 将
jasmine
添加到 tsconfig.json 的types
- 从 tsconfig.spec.json 中删除
- 重新安装我项目的所有依赖项
- 在我的 monorepo 的根目录中将
@types/jasmine
types
定义
jasmine
添加到 tsconfig.json 的 types
自己试试
如果你想仔细看看你可以通过
自己尝试一下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';