删除@types/jest后如何使用jest.mock(^jest@24)

How to use jest.mock after @types/jest is removed (^jest@24)

我最近将 @angular-builders/jest 从 7 更新到 8。在 migration guide it states that I should remove @types/jest because it's now comes with Jest v24. (I also updated my 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,哪种有意义。

In tsconfig.json (root directory, used by IDE):

Remove typeRoots array

Again, since Jest typings are packaged inside jest package and are not located under node_modules/@types you don't want the type roots to be limited to a specific folder.

但是有什么用呢?迁移指南说我应该删除 @types/jest 但如何让它与 jest.mockjest.clearAllMocks 再次兼容?

我试过了:

import { mock } from 'jest'; // mock isn't found
import * as jest from 'jest'; // jest.mock isn't found

请指教

这是我的配置(与 simple-app example 相同)

相关开发包

{
    "@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": {}
        }
      }
    }
  }
}

@types/jest 显然 still needed. The migration guide 已进行调整以反映这一点。

配置现在看起来像这样:

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"]
}