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: B
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.
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!
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"]
}