Skip to content

fix(vitest): handle zoneless Angular apps in vitest configuration generator#34700

Merged
leosvelperez merged 1 commit intomasterfrom
gh-33983
Mar 6, 2026
Merged

fix(vitest): handle zoneless Angular apps in vitest configuration generator#34700
leosvelperez merged 1 commit intomasterfrom
gh-33983

Conversation

@leosvelperez
Copy link
Member

Current Behavior

When running @nx/vitest:configuration on an Angular project, the generator always generates import '@analogjs/vitest-angular/setup-zone' regardless of whether the app is zoneless. Additionally, the setup file generation logic for Angular 21+ (which uses setupTestBed()) only exists in packages/angular/src/generators/utils/add-vitest.ts (createAnalogSetupFile), making the vitest generator not self-contained.

Expected Behavior

The vitest configuration generator should:

  • Auto-detect whether an Angular project is zoneless (by checking polyfills for apps, or zone.js dependency for libraries)
  • Generate setup-snapshots instead of setup-zone for zoneless projects
  • Handle Angular 21+ setupTestBed() setup directly, without requiring a separate function in the angular package
  • Accept an explicit zoneless option to override auto-detection

Related Issue(s)

Fixes #33983

@leosvelperez leosvelperez self-assigned this Mar 4, 2026
@netlify
Copy link

netlify bot commented Mar 4, 2026

Deploy Preview for nx-dev ready!

Name Link
🔨 Latest commit 1bbf497
🔍 Latest deploy log https://app.netlify.com/projects/nx-dev/deploys/69aa91a476b29d00081fd5d5
😎 Deploy Preview https://deploy-preview-34700--nx-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link

netlify bot commented Mar 4, 2026

Deploy Preview for nx-docs ready!

Name Link
🔨 Latest commit 1bbf497
🔍 Latest deploy log https://app.netlify.com/projects/nx-docs/deploys/69aa91a467e24d0008b3ee11
😎 Deploy Preview https://deploy-preview-34700--nx-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@nx-cloud
Copy link
Contributor

nx-cloud bot commented Mar 4, 2026

View your CI Pipeline Execution ↗ for commit 1bbf497

Command Status Duration Result
nx affected --targets=lint,test,build,e2e,e2e-c... ✅ Succeeded 55m 33s View ↗
nx run-many -t check-imports check-lock-files c... ✅ Succeeded 3m 22s View ↗
nx-cloud record -- nx-cloud conformance:check ✅ Succeeded 8s View ↗
nx-cloud record -- nx format:check ✅ Succeeded 1s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded <1s View ↗

☁️ Nx Cloud last updated this comment at 2026-03-06 09:34:52 UTC

Copy link
Contributor

@nx-cloud nx-cloud bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

At least one additional CI pipeline execution has run since the conclusion below was written and it may no longer be applicable.

Nx Cloud is proposing a fix for your failed CI:

These changes update the test snapshots to align with the new Angular 21+ vitest configuration behavior introduced in the PR. The generator now defaults to Angular 21+ when no version is specified, using setup-snapshots and setupTestBed() instead of the legacy Angular 20 APIs. We've updated the snapshots to expect the new format and removed an extra blank line in the Angular 19 test to match actual generator output.

Warning

We could not verify this fix.

Suggested Fix changes
diff --git a/packages/vite/src/generators/vitest/__snapshots__/vitest.spec.ts.snap b/packages/vite/src/generators/vitest/__snapshots__/vitest.spec.ts.snap
index 1124ae79ca..a1d15f014e 100644
--- a/packages/vite/src/generators/vitest/__snapshots__/vitest.spec.ts.snap
+++ b/packages/vite/src/generators/vitest/__snapshots__/vitest.spec.ts.snap
@@ -2,18 +2,10 @@
 
 exports[`vitest generator angular should generate src/test-setup.ts 1`] = `
 "import '@angular/compiler';
-import '@analogjs/vitest-angular/setup-zone';
+import '@analogjs/vitest-angular/setup-snapshots';
+import { setupTestBed } from '@analogjs/vitest-angular/setup-testbed';
 
-import {
-  BrowserTestingModule,
-  platformBrowserTesting,
-} from '@angular/platform-browser/testing';
-import { getTestBed } from '@angular/core/testing';
-
-getTestBed().initTestEnvironment(
-  BrowserTestingModule,
-  platformBrowserTesting(),
-);
+setupTestBed({ zoneless: false });
 "
 `;
 
diff --git a/packages/vite/src/generators/vitest/vitest.spec.ts b/packages/vite/src/generators/vitest/vitest.spec.ts
index 43e3b2ce75..2fa6cc58a6 100644
--- a/packages/vite/src/generators/vitest/vitest.spec.ts
+++ b/packages/vite/src/generators/vitest/vitest.spec.ts
@@ -235,7 +235,6 @@ describe('vitest generator', () => {
       expect(tree.read('apps/my-test-angular-app/src/test-setup.ts', 'utf-8'))
         .toMatchInlineSnapshot(`
         "import '@analogjs/vitest-angular/setup-zone';
-
         import {
           BrowserDynamicTestingModule,
           platformBrowserDynamicTesting,

🔔 Heads up, your workspace has pending recommendations ↗ to auto-apply fixes for similar failures.

✅ The fix was applied locally; push changes to this branch.

View interactive diff ↗



🎓 Learn more about Self-Healing CI on nx.dev

@leosvelperez leosvelperez marked this pull request as ready for review March 5, 2026 07:12
@leosvelperez leosvelperez requested review from a team as code owners March 5, 2026 07:12
@leosvelperez leosvelperez requested a review from jaysoo March 5, 2026 07:12
…erator

Add `zoneless` option to vitest configuration schema with auto-detection
from project config. Generate appropriate setup file based on Angular
version and zoneless status: use `setup-snapshots` for zoneless apps
instead of `setup-zone`, and use `setupTestBed()` for Angular 21+.

Remove duplicated `createAnalogSetupFile` from `addVitestAnalog` and
pass `zoneless` through to the vitest configuration generator instead.
@leosvelperez leosvelperez merged commit fb8884e into master Mar 6, 2026
23 checks passed
@leosvelperez leosvelperez deleted the gh-33983 branch March 6, 2026 11:37
@github-actions
Copy link
Contributor

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Angular vitest setup does include setup-zone for zoneless applications

2 participants