I have an Angular 7 app with 3300+ tests. And they take too much time to run and too much effort to maintain. I've heard a lot of good things about Jest but, not sure if migrating all of those tests from Jasmine to Jest is actually feasible.
You can easily answer this yourself by upgrading to jest. There's a great walkthrough on Github by the jest-preset-angular team.
In your project root add a few files:
setup.jest.ts with:
import 'jest-preset-angular/setup-jest';
jest.config.js with:
// Angular Ivy (9+ only for the next line)
require('jest-preset-angular/ngcc-jest-processor');
// jest.config.js
module.exports = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
};
tsconfig.spec.json with:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": ["jest"]
},
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"]
}
modify tsconfig.json:
"lib": [ "es2018", "dom" ],
"esModuleInterop": true
modify tsconfig.spec.json:
"types": [ "jest" ] // Replace "jasmine"
modify package.json
"test": "jest -c ./jest.config.js ./src
add required packages:
npm install jest jest-preset-angular @types/jest @angular-builders/jest
You can then automatically transform your tests (to some degree) from Jasmine to Jest:
npx jest-codemods
Run:
npm run test
See how many failing tests you will need to manually fix to complete the migration. And decide if this is within your timelines to do it.
Error: cannot set base providers because it has already been called
Look for tests.ts file and remove it. This is a karma file that you don't need in Jest.
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