Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it feasible to migrate from Jasmine/Karma to Jest?

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.

like image 767
Crixx93 Avatar asked Nov 24 '25 19:11

Crixx93


1 Answers

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

Post install migration

You can then automatically transform your tests (to some degree) from Jasmine to Jest:

npx jest-codemods

Now you can answer your question for your particular project

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.

Troubleshooting

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.

like image 87
P.Brian.Mackey Avatar answered Nov 26 '25 16:11

P.Brian.Mackey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!