【发布时间】:2026-02-01 13:45:01
【问题描述】:
问题
我有一个 Angular 9 应用程序,并且刚刚向组件添加了一个小单元测试。但是 VSCode 无法识别 describe、beforeEach、it 等类型。虽然我有一个tsconfig.spec.json 文件,其中jasmine 在其types 定义中:
{
"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添加到types的tsconfig.json - 从tsconfig.spec.json 中删除
types定义 - 重新安装我项目的所有依赖项
- 将
jasmine添加到我的monorepo 根目录中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找不到类型的问题。
【问题讨论】:
标签: angular typescript unit-testing visual-studio-code jasmine