Skip to content

Conversation

@elfelli
Copy link

@elfelli elfelli commented Oct 25, 2025

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-test builder without requiring third-party dependencies like @angular-builders/jest, making it a cleaner and more streamlined alternative.

Changes

User-Facing Changes

  • Angular users are now prompted during generation to choose between Jest (default) and Vitest
  • New option: --client-test-framework (jest | vitest)
  • Jest remains the default for backward compatibility

Implementation Details

Modified Files:

  • generators/client/command.ts - Added clientTestFramework configuration option with prompt (Angular only)
  • generators/client/application.ts - Added conditional logic to derive test framework and boolean properties
  • generators/angular/files-angular.ts - Conditional file generation based on test framework choice
  • generators/angular/resources/package.json - Added vitest (4.0.2) and jsdom (26.0.0) dependencies
  • generators/angular/templates/angular.json.ejs - Conditional builder configuration
  • generators/angular/templates/angular.json.esbuild.ejs - Conditional builder configuration (esbuild variant)
  • generators/angular/templates/package.json.ejs - Conditional dependencies and scripts

New Template Files:

  • generators/angular/templates/vitest.config.ts.ejs - Vitest configuration with coverage, reporters, and exclusions
  • generators/angular/templates/tsconfig.vitest.json.ejs - TypeScript configuration for Vitest tests
  • generators/angular/templates/src/main/webapp/app/test-setup.ts.ejs - Test bootstrap file initializing Angular TestBed with zone.js

Technical Approach

  • Uses Angular's official @angular/build:unit-test builder with runner: "vitest"
  • Conditionally generates test framework files (Jest: jest.conf.js, tsconfig.spec.json | Vitest: vitest.config.ts, tsconfig.vitest.json, test-setup.ts)
  • Conditionally includes dependencies (Jest packages or Vitest + jsdom)
  • Works with both webpack and esbuild bundlers
  • No impact on Vue (continues to use Vitest) or React (continues to use Jest)

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 lint passes with no warnings

Backward Compatibility

✅ Jest remains the default for Angular applications
✅ No breaking changes to existing functionality
✅ Vue and React frameworks unaffected
✅ Existing .yo-rc.json configurations without clientTestFramework will default to Jest

Checklist

  • Code follows project conventions and style
  • Lint passes without warnings
  • Manual testing completed for all scenarios
  • Backward compatibility maintained
  • No breaking changes introduced
  • Documentation updated (commit message provides context)

Related Issue

Fix #31134

Screenshots/Examples

Generated with Vitest:

"test": {
  "builder": "@angular/build:unit-test",
  "options": {
    "runner": "vitest",
    "tsConfig": "tsconfig.vitest.json",
    "buildTarget": "::development"
  }
}

Generated with Jest (default):

"test": {
  "builder": "@angular-builders/jest:run",
  "options": {
    "configPath": "jest.conf.js",
    "tsConfig": "tsconfig.spec.json"
  }
}

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.

Eslem FELLI added 3 commits October 26, 2025 20:59
- Add clientTestFramework option to client generator (jest/vitest)
- Angular users can now choose between Jest and Vitest during generation
- Create Vitest configuration templates (vitest.config.ts, test-setup.ts, tsconfig.vitest.json)
- Update angular.json templates to conditionally use @angular/build:unit-test builder for Vitest
- Update package.json template to conditionally include test framework dependencies
- Add vitest and jsdom to Angular resources/package.json
- Modify files-angular.ts to conditionally generate test configuration files
- Jest remains the default for backward compatibility

Fix jhipster#31134
@elfelli elfelli force-pushed the feature/angular-vitest-support branch from 584874f to b663690 Compare October 26, 2025 19:59
@mshima
Copy link
Member

mshima commented Oct 27, 2025

Vitest support is already in the works.
Zone.js incompatibility landed here #31154.
Mock behavior difference landed here #31156
Vitest support is almost ready here #31135

@mshima
Copy link
Member

mshima commented Oct 27, 2025

Thanks for your contribution.

@mshima
Copy link
Member

mshima commented Oct 27, 2025

Angular doesn’t support vitest.config file generators/angular/templates/vitest.config.ts.ejs

@elfelli elfelli closed this Oct 27, 2025
@elfelli elfelli deleted the feature/angular-vitest-support branch October 27, 2025 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Angular] Add Vitest as alternative to Jest.

2 participants