Add Vitest as Alternative to Jest for Angular #31158
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add Vitest as Alternative to Jest for Angular
Description
This PR implements support for Vitest as an alternative testing framework to Jest for Angular applications, as requested in issue #31134.
Angular now provides experimental Vitest support through the official
@angular/build:unit-testbuilder without requiring third-party dependencies like@angular-builders/jest, making it a cleaner and more streamlined alternative.Changes
User-Facing Changes
--client-test-framework(jest|vitest)Implementation Details
Modified Files:
generators/client/command.ts- AddedclientTestFrameworkconfiguration option with prompt (Angular only)generators/client/application.ts- Added conditional logic to derive test framework and boolean propertiesgenerators/angular/files-angular.ts- Conditional file generation based on test framework choicegenerators/angular/resources/package.json- Addedvitest(4.0.2) andjsdom(26.0.0) dependenciesgenerators/angular/templates/angular.json.ejs- Conditional builder configurationgenerators/angular/templates/angular.json.esbuild.ejs- Conditional builder configuration (esbuild variant)generators/angular/templates/package.json.ejs- Conditional dependencies and scriptsNew Template Files:
generators/angular/templates/vitest.config.ts.ejs- Vitest configuration with coverage, reporters, and exclusionsgenerators/angular/templates/tsconfig.vitest.json.ejs- TypeScript configuration for Vitest testsgenerators/angular/templates/src/main/webapp/app/test-setup.ts.ejs- Test bootstrap file initializing Angular TestBed with zone.jsTechnical Approach
@angular/build:unit-testbuilder withrunner: "vitest"jest.conf.js,tsconfig.spec.json| Vitest:vitest.config.ts,tsconfig.vitest.json,test-setup.ts)Testing
✅ Angular + Vitest: Verified correct generation with proper builder, config files, and dependencies
✅ Angular + Jest (default): Verified no regression, works as before
✅ Vue framework: Verified still defaults to Vitest
✅ React framework: Verified still defaults to Jest
✅ Linting:
npm run lintpasses with no warningsBackward Compatibility
✅ Jest remains the default for Angular applications
✅ No breaking changes to existing functionality
✅ Vue and React frameworks unaffected
✅ Existing
.yo-rc.jsonconfigurations withoutclientTestFrameworkwill default to JestChecklist
Related Issue
Fix #31134
Screenshots/Examples
Generated with Vitest:
Generated with Jest (default):
Additional Notes
This implementation follows the pattern established by Vue's Vitest integration while adapting it for Angular's specific requirements (TestBed initialization, zone.js, official Angular CLI builder).
The Vitest support is marked as experimental in the prompt text to align with Angular's official documentation.