我最近将@angular-builders/jest
7 更新到 8。在迁移指南中,它指出我应该删除它,@types/jest
因为它现在随 Jest v24 一起提供。(我还更新了我的 tsconfig)
到现在为止还挺好。
但是现在我的测试失败了
error TS2304: Cannot find name 'jest'.
jest.mock('src/app/omitted');
error TS2304: Cannot find name 'jest'.
jest.clearAllMocks();
而 VS Code 与 jest 全局变量无关。根据迁移指南,当我从 tsconfig 中删除 typeRoots 时,哪种有意义。
在 tsconfig.json(根目录,IDE 使用)中:
删除 typeRoots 数组
同样,由于 Jest 类型被打包在 jest 包中并且不位于 node_modules/@types 下,因此您不希望类型根被限制在特定文件夹中。
但是什么给了?迁移指南说我应该删除@types/jest
,但我如何让它一次又一次地玩得jest.mock
很好jest.clearAllMocks
?
我试过:
import { mock } from 'jest'; // mock isn't found
import * as jest from 'jest'; // jest.mock isn't found
请指教。
这是我的配置(与简单应用示例相同)
相关开发包
{
"@angular-builders/jest": "^8.0.3",
"jest": "^24.8.0",
"jest-preset-angular": "^7.1.1",
"ts-jest": "^24.0.2",
}
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "esnext",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"lib": ["es2018", "dom"],
"strict": true // note strict mode
}
}
tsconfig.spec.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"allowJs": true
},
"files": ["src/polyfills.ts"],
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}
角.json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"my-project": {
//...
"architect": {
//...
"test": {
"builder": "@angular-builders/jest:run",
"options": {}
}
}
}
}
}