As an Angular developer I want to get rid of Karma/Jasmine and use Jest instead so that I can test my application fast and without any pain. Unfortunately, I ran into unexpected problems.
In the last few hours, I went through all those tutorials showing up on top of the SERPs:
- Testing Angular Faster with Jest - xfive.co (recommended by jestjs.io)
- Integrate Jest into an Angular application and library - angularindepth.com
- How to use Jest in Angular aka make unit testing great (again) - itnext.io
- Use Jest instead of Karma and Jasmine for your Angular project - upcoding.fr
Here is what I did in detail:
- Create a new Angular application using
ng new my-application. - Change directory:
cd my-application. - Run
npm start(ng serve) andnpm test(ng test) to see if everything works - it works. - Install Jest using
npm install --save-dev jest jest-preset-angular @types/jest - Add this to
package.json:
{
...
"jest": {
"preset": "jest-preset-angular",
"setupFilesAfterEnv": ["<rootDir>/src/setupJest.ts"]
}
}
- Change the npm scripts in
package.jsonto the following:
"test": "jest",
"test:watch": "jest --watch",
- Into the folder
srccreate a filesetupJest.tswith the following content:
import 'jest-preset-angular';
The problem:
When I run npm test now, the test suite fails to run with
File not found: <rootDir>/tsconfig.spec.json
I have no idea what I can do about that because all those tutorials from above do not seem to handle this issue.
Please help me!
Thank you in advance!