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.
To solve the "Cannot find name 'it'" jest error, make sure to install the typings for jest - npm i -D @types/jest and add them to the types array in your tsconfig.json file. TypeScript has to be picking up the directory in which your tests are located. Here is an example of how the error occurs. Copied! // Cannot find name 'it'.
If you have attached multiple projects, then all TS projects should have in tsconfig the "jest" in types. That means that the Jest tests are affected by the ts config of other projects! That's not expected. >That's not expected. That's expected unless your attached projects have a common root dir with tsconfig.json in it.
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'.
Incidentally, jest-preset-angular 7 does requires jest 24.1 as peer dependency and version 6 works only on jest 23. But so far I've just ignored the warning. If you have attached multiple projects, then all TS projects should have in tsconfig the "jest" in types. That means that the Jest tests are affected by the ts config of other projects!
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"]
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With