12

I am using Angular 7 and Jest. When I was running jest with --codecoverage, all my tests passed but many branches in the constructor were not covered (similar to: Branches on constructor not covered).

I spent quite a lot of time checking everywhere how to solve it and it seemed I had to upgrade jest-preset-angular to version 7. After I did it, all my test suites fail to run with the following message:

src/app/whatever.component.spec.ts
 ● Test suite failed to run
    
 TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
 error TS2688: Cannot find type definition file for 'jasmine'.

I've been looking around and I tried to apply the solutions I found (such as Cannot find type definition file for 'jasmine', ERROR in error TS2688: Cannot find type definition file for 'jest', ...) but none worked.

I copy here some of the dependencies I have in package.json:

    "jest": "^24.1.0",
    "jest-junit": "^6.3.0",
    "jest-preset-angular": "^7.0.1",
    "ts-node": "~7.0.1",
    "tslint": "~5.9.1",
    "typescript": "3.2.4"

My previous version was:

    "jest-preset-angular": "^6.0.2",

Update: I started my project with Angular 6 and Jasmine and I updated it to Angular 7 and Jest. I don't know if this is an important information.

E_net4
  • 27,810
  • 13
  • 101
  • 139
xavier
  • 1,860
  • 4
  • 18
  • 46

4 Answers4

14

Go to tsconfig.spec.json in the types field under the compilerOptions and remove jasmine and add jest

"compilerOptions": {
"module": "commonjs",
"outDir": "./out-tsc/spec",
"types": ["jest", "node"]
}
Alex Myers
  • 6,196
  • 7
  • 23
  • 39
Emmy Omega
  • 216
  • 2
  • 4
3

I finally solved my problem. The solution is based on @Emmy Omega’s answer (that’s why I give him the bounty) and @Akshay Rana’s comment, but had to do some more things. I had jasmine indeed in my tsconfig.spec.json file, but changing it with json (I had already tried it) didn’t fix the problem. To make it work I followed the following steps:

Update npm (I had an old version and I had some problems installing packages).

Remove all Karma related stuff that was still there:

npm remove karma karma-chrome-launcher karma-coverage-istanbul-reporter karma-jasmine karma-jasmine-html-reporter

• I still had this test.ts file around and I deleted it (I don’t know where it came from). I also remove it from tsconfig.app.json and tsconfig.spec.json.

• In tsconfig.spec.json I changed one occurrence of jasmine with jest (as suggested).

• Updated package.json with "jest-preset-angular": "^7.0.1

• Delete all packages in node_modules folder and reinstall them all again with npm i.

I don’t know exactly which of these steps made the difference but it finally worked!

xavier
  • 1,860
  • 4
  • 18
  • 46
1

I was getting the following error when trying to run Jest in my Angular project.

TypeScript diagnostics (customize using [jest-config].globals.ts-jest.diagnostics option): error TS2688: Cannot find type definition file for 'jquery'. error TS2688: Cannot find type definition file for 'jsdom'.

For me I had to add the type definition packages.

yarn add @types/jquery @types/jsdom --dev

After that these errors stopped.

Dino
  • 7,779
  • 12
  • 46
  • 85
Stephen Cooper
  • 1,069
  • 7
  • 15
0

I had same problem but as suggested I changed jasminewith jest of tsconfig.spec.json

then i changed target under same file from es5 to es2018 and bam

PSP2912
  • 103
  • 1
  • 7