Migrating From Jest to Vitest
1.a. ๐พ Use Both Jest & Vitestโ
In the rare occurrence where some tests are too coupled to Jest and hard to automatically migrate to Vitest, you can keep the Jest's configuration files and run some specific tests with Jest and others with Vitest. This will help you migrate progressively. Otherwise, you can simply remove the Jest setup.
Backup Jest's test-setup.ts fileโ
git mv {MY_PROJECT}/src/test-setup.ts {MY_PROJECT}/src/test-setup-jest.ts
Update jest.config.ts fileโ
Update the jest.config.ts configuration to use the test-setup-jest.ts file and match test files ending with .jest.spec.ts.
- setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
+ setupFilesAfterEnv: ['<rootDir>/src/test-setup-jest.ts'],
+ testRegex: '(/__tests__/.*|(\\.|/)jest\.(test|spec))\\.[jt]sx?$',
Rename test Targetโ
Rename the test target to jest in project.json (if you are using Nx) or angular.json (if you are using Angular CLI).
"architect|targets": {
- "test": {
+ "jest": {
"builder": "...",
...
}
...
}
You will be able to run the Jest tests with the following command:
nx jest {MY_PROJECT}
# or
ng run {MY_PROJECT}:jest
1.b. ๐๏ธ Remove Jestโ
Remove Jest configuration files:
rm -f {MY_PROJECT}/jest.config.ts {MY_PROJECT}/src/test-setup.ts
Remove the test target from project.json (if you are using Nx) or angular.json (if you are using Angular CLI).
"architect|targets": {
- "test": {...}
...
}
2. ๐ฆ Set Up Vitestโ
For Nx Usersโ
If you are using Nx (>= 20.3.0), you can run the following commands to set up Vitest in your Angular project:
nx add @nx/vite
nx g vitest --project {MY_PROJECT}
For Angular CLI Usersโ
If you are using the Angular CLI (>= 21.0.0), you can use the new unit-test builder to set up Vitest in your Angular project.
Otherwise, if you are using an older Angular CLI version or if you want more control over the setup or more Vitest features, you can use Angular Vitest Generator/Schematic by Analog to set up Vitest in your Angular project.
Using the Angular CLI's unit-test builderโ
Update your angular.json file to use the unit-test builder:
{
...
"targets": {
+ "test": {
+ "builder": "@angular/build:unit-test",
+ }
}
}
Vitest is the default test runner when using the unit-test builder.
Using the Angular Vitest Generator/Schematic by Analogโ
ng g @analogjs/platform:setup-vitest --project {MY_PROJECT}
Note that this does not set up anything specific to Analog. It is just a convenient way to set up Vitest in your Angular project.
The Analog team did an outstanding job of making it easy to use Vitest with Angular projects.
Update src/test-setup.tsโ
You may need to update the src/test-setup.ts file to apply whatever previous setup you had in Jest to Vitest.
3. ๐งณ Migrate Testsโ
Vitest shares a large API surface with Jest, so most of the tests should work without any changes.
However, if your tests are using Jest-specific APIs (e.g. jest.fn()), you may need to update them.
In general, I'd recommend staying as decoupled as possible from the testing framework. For instance, it is better to use fakes than mocks or spies. Cf. ๐บ Fake it till you Mock it.