Skip to content

Conversation

@yjaaidi
Copy link
Contributor

@yjaaidi yjaaidi commented Dec 7, 2025

Vitest Browser Mode caches modules and does not re-execute side-effects.
Therefore, when hooks are cleared, they are not set up again so TestBed
is not reset after each test.

Vitest was fixed to allow side-effects on setup files by
not caching them (see vitest-dev/vitest#8884),
but the behavior is not recursive so hooks that are
registered as side effects in other files such as
@analogjs/vitest-angular/setup-testbed.ts will not work.

The solution is to register these hooks inside the setUsetupTestBed function.

@netlify
Copy link

netlify bot commented Dec 7, 2025

Deploy Preview for analog-docs ready!

Name Link
🔨 Latest commit a8d9be0
🔍 Latest deploy log https://app.netlify.com/projects/analog-docs/deploys/6936a6d42cbf98000885a308
😎 Deploy Preview https://deploy-preview-1995--analog-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.

@netlify
Copy link

netlify bot commented Dec 7, 2025

Deploy Preview for analog-app ready!

Name Link
🔨 Latest commit a8d9be0
🔍 Latest deploy log https://app.netlify.com/projects/analog-app/deploys/6936a6d4280240000879a505
😎 Deploy Preview https://deploy-preview-1995--analog-app.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 Dec 7, 2025

Deploy Preview for analog-blog ready!

Name Link
🔨 Latest commit a8d9be0
🔍 Latest deploy log https://app.netlify.com/projects/analog-blog/deploys/6936a6d4e1fc0d00084d3939
😎 Deploy Preview https://deploy-preview-1995--analog-blog.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.

@yjaaidi yjaaidi force-pushed the fix/reset-test-bed-between-tests-in-browser-mode branch from c63abea to b24883a Compare December 7, 2025 01:08
export function setupTestBed(
options: TestBedSetupOptions = { zoneless: true, providers: [] },
) {
beforeEach(getCleanupHook(false));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here, we're sure the hooks will be registered.

Copy link
Member

Choose a reason for hiding this comment

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

Did you see any tests slow down by putting them here? Or does it have no impact with the Vitest fixes

Copy link
Contributor Author

@yjaaidi yjaaidi Dec 8, 2025

Choose a reason for hiding this comment

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

No slowdown observed, and it shouldn't slow down anything.

@brandonroberts brandonroberts changed the title fix: reset TestBed between tests in Vitest Browser Mode fix(vitest-angular): reset TestBed between tests in Vitest Browser Mode Dec 7, 2025
@brandonroberts
Copy link
Member

@yjaaidi just needs merge conflicts resolved.

Vitest Browser Mode caches modules and does not re-execute side-effects.
Therefore, when hooks are cleared, they are not set up again so TestBed
is not reset after each test.

Vitest was fixed to allow side-effects on setup files by
not caching them (see vitest-dev/vitest#8884),
but the behavior is not recursive so hooks that are
registered as side effects in other files such as
`@analogjs/vitest-angular/setup-testbed.ts` will not work.

The solution is to register these hooks inside the `setUsetupTestBed` function.

Closes analogjs#1994
@yjaaidi yjaaidi force-pushed the fix/reset-test-bed-between-tests-in-browser-mode branch from 1baf01a to a8d9be0 Compare December 8, 2025 10:22
@yjaaidi
Copy link
Contributor Author

yjaaidi commented Dec 8, 2025

@yjaaidi just needs merge conflicts resolved.

Really weird, I can't reproduce the conflict locally 😅.
I've reset it to main and updated it, but github is still complaining.
Seems like some caching issue or I'm getting something wrong.

Any clues? Should I open another PR?

Update: 🤦🏻‍♂️ I was targeting beta instead of main

@yjaaidi
Copy link
Contributor Author

yjaaidi commented Dec 8, 2025

P.S.: I've updated the tests to avoid false negatives where the tests could pass when using multiple workers, or disabling/removing other tests such as the aot one. We need two test files to reproduce the bug.

@yjaaidi yjaaidi changed the base branch from beta to main December 8, 2025 10:45
@brandonroberts brandonroberts merged commit 66a2b6c into analogjs:main Dec 8, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants