Using Jest I get: Cannot find type definition file for 'jasmine'

xavier

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",

I don't know which additional information I can provide. If there is something missing I'd appreciate if you could tell me that before (actually: instead of) down voting...

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.

Thank you!

xavier

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!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ERROR in error TS2688: Cannot find type definition file for 'jest'

After typescript update 2.3.4 => 2.4.2 i get compilation error "Cannot find type definition file for 'reflect-metadata'."

Cannot find type definition file for 'ramda'

Build:Cannot find type definition file for 'node'

Angular 7: cannot find the type definition file

Cannot find type definition file for 'core-js'

error TS2688: Cannot find type definition file for 'dist'

error TS2688: Cannot find type definition file for 'bson'

error TS2688: Cannot find type definition file for 'ioredis'

Compile on heroku node ts- jest types- cannot find type definition

jasmine-expect cannot find jest, jasmine v2.x, or jasmine v1.x

XML file, XSD schema Errors: Cannot resolve the name to a(n) 'type definition', Cannot find the declaration of element

Jasmine/Jest type conflicts

Jest fails when trying to find type definition in node_modules

Typescript cannot find method in definition file

TS2688: Cannot find type definition file for 'node' with @types/node installed

Cannot find type definition file for 'redux-logger' error in tsconfig.json

TypeScript error in undefined(undefined,undefined): Cannot find type definition file for 'src'. TS2688

Jasmine custom matcher type definition

I get the error :[WinError 2] The system cannot find the file specified:

How do I find the SourceLocation of the type definition of a VarDecl using the Clang AST?

Using Typescript type definition from another file

Where can I find the type definition of the Highcharts.AnnotationsOptions type?

How can I find the mime type without charset using "file"?

How can I test a change handler for a file-type input in React using Jest/Enzyme?

Cannot find the definition of a constant

TypeScript Definition File Cannot Find .d.ts

I cannot find the definition of a method in node.js source code

Cannot find module '_debugger' while using jasmine-debug or jasmine-node-debug