Skip to content

Conversation

@clydin
Copy link
Member

@clydin clydin commented Nov 9, 2025

This commit refines the Angular TestBed initialization within the Vitest runner to ensure correct behavior in non-isolated test environments (isolate: false).

Previously, in a non-isolated setup, the TestBed initialization logic in the virtual setup file could be problematic if the file was evaluated multiple times. This could lead to re-registering beforeEach/afterEach hooks or attempting to re-initialize the TestBed, causing errors or unexpected test behavior.

This change introduces a globally unique symbol guard (Symbol.for('@angular/cli/testbed-setup')) that ensures the entire TestBed setup block (including hook registration and initTestEnvironment) is executed only once per test run. This prevents redundant executions and potential errors.

Finally, the isolate option in Vitest is now explicitly defaulted to false when not specified by the user, aligning with the traditional Karma/Jasmine experience.

@clydin clydin requested a review from alan-agius4 November 9, 2025 14:36
@clydin clydin added the target: rc This PR is targeted for the next release-candidate label Nov 9, 2025
@clydin clydin added the action: review The PR is still awaiting reviews from at least one requested reviewer label Nov 9, 2025
@yjaaidi
Copy link
Contributor

yjaaidi commented Nov 9, 2025

the isolate option in Vitest is now explicitly defaulted to false

🚀 awesome! Thanks Charles!

…itest

This commit refines the Angular TestBed initialization within the Vitest runner to ensure correct behavior in non-isolated test environments (`isolate: false`).

Previously, in a non-isolated setup, the TestBed initialization logic in the virtual setup file could be problematic if the file was evaluated multiple times. This could lead to re-registering `beforeEach`/`afterEach` hooks or attempting to re-initialize the TestBed, causing errors or unexpected test behavior.

This change introduces a globally unique symbol guard (`Symbol.for('@angular/cli/testbed-setup')`) that ensures the entire TestBed setup block (including hook registration and `initTestEnvironment`) is executed only once per test run. This prevents redundant executions and potential errors.

Finally, the `isolate` option in Vitest is now explicitly defaulted to `false` when not specified by the user, aligning with the traditional Karma/Jasmine experience.
@alan-agius4 alan-agius4 added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Nov 10, 2025
@alan-agius4 alan-agius4 merged commit 377780a into angular:main Nov 10, 2025
31 of 32 checks passed
@alan-agius4
Copy link
Collaborator

This PR was merged into the repository. The changes were merged into the following branches:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: @angular/build target: rc This PR is targeted for the next release-candidate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unit-test builder setup files in vitest and browser mode do not get the right instance of TestBed

3 participants