最近,我将@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代码与jest全局变量无关。根据迁移指南,当我从tsconfig中删除typeRoots时,哪种方式是有意义的。
在tsconfig.json (根目录,由IDE使用)中: 删除typeRoots数组 同样,由于Jest类型被打包在jest包中,而不是位于节点_模块/@类型下,所以不希望类型根被限制在特定的文件夹中。
但是是什么原因呢?迁移指南说我应该删除@types/jest,但是如何使它再次与jest.mock和jest.clearAllMocks保持良好关系呢?
我试过:
import { mock } from 'jest'; // mock isn't found
import * as jest from 'jest'; // jest.mock isn't found请指点。
以下是我的心声(和简单-app示例一样)
相关开发包
{
"@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"]
}angular.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": {}
}
}
}
}
}发布于 2019-06-24 11:02:36
@types/jest显然是仍需。对迁移指南进行了调整,以反映这一点。
这些吐露现在看起来是这样的:
package.json
"@angular-builders/jest": "^8.0.3",
"@types/jest": "^24.0.15",
"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"],
"types": ["jest"],
"strict": true // note strict mode, this isn't default
}
}tsconfig.spec.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec"
},
"files": ["src/polyfills.ts"],
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}https://stackoverflow.com/questions/56663876
复制相似问题