angular-builders: Cannot find type definitions for jest

Describe the bug

After following the steps in Readme TS is throwing as it can’t find any of the global types.

To Reproduce Steps to reproduce the behavior:

  1. Create a new Angular CLI app (ng new app)
  2. Add Jest as described in the Jest builder Readme
  3. Update an spec to use a jest matcher, such as resolve for example.
  4. npm ng test
  5. See error

Example

jest-error.zip

Expected behavior

There should be no errors.

Builder:

  • Jest builder
  • 8.0.2

Libraries

  • angular-devkit/build-angular: ~0.800.0:

Additional context

package.json
{
  "name": "jest-error",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~8.0.0",
    "@angular/common": "~8.0.0",
    "@angular/compiler": "~8.0.0",
    "@angular/core": "~8.0.0",
    "@angular/forms": "~8.0.0",
    "@angular/platform-browser": "~8.0.0",
    "@angular/platform-browser-dynamic": "~8.0.0",
    "@angular/router": "~8.0.0",
    "rxjs": "~6.4.0",
    "tslib": "^1.9.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-builders/jest": "^8.0.2",
    "@angular-devkit/build-angular": "~0.800.0",
    "@angular/cli": "~8.0.1",
    "@angular/compiler-cli": "~8.0.0",
    "@angular/language-service": "~8.0.0",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "jest": "^24.8.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.4.3"
  }
}

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 23 (20 by maintainers)

Most upvoted comments

Ok, that’s actually my bad. The README is not up to date.
The root cause of this problem is that Jest@24 comes with built in types and while I removed the “install @types/jest” step from the setup steps I forgot to update the tsconfig.json section.
Adding “types: [“jest”]” is what causes the issues. You can safely remove it from both tsconfigs (spec and root) and then you’ll be able to use jest without @types/jest. Moreover I’d strongly advise you to remove it once you removed the types entry from tsconfigs, because these are an obsolete typings.

Ok, seems that my assumption about @types/jest being deprecated was wrong. Apparently there is still a need in this. I’m going to update the migration guide and the docs, meanwhile you can npm install -D @types/jest, get the typeRoots back and add types: ["jest"] to tsconfig.json and tsconfig.spec.json.