Skip to content

Cannot set base providers because it has already been called when running >1 test cases with Vitest ^4.0.5 #32754

Description

@thanh-nguyen-codeleap

Which @angular/* package(s) are the source of the bug?

Don't known / other

Is this a regression?

Yes

Description

When running an Angular test suite with @angular/platform-browser/testing@21.2.1 and vitest@4.0.18, the test runner throws the following error after the second test case:

Error: Cannot set base providers because it has already been called
The error originates from Angular's TestBed.initTestEnvironment() being invoked multiple times within the same worker context. Vitest ^4.0.5 appears to have changed how setupFiles and worker isolation interact, causing the test-setup.ts to be re-executed for subsequent test files while the Angular TestBed state is already initialized from the previous run.

The issue only surfaces when running 2 or more test files/cases on Github Actions. Running 1 passes without error.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

Error: Cannot set base providers because it has already been called
   ❯ _TestBedImpl.initTestEnvironment ../../node_modules/.pnpm/k8-fastbuild-ST-fdfa778d11ba/bin/packages/core/testing/src/test_bed.ts:477:13
   ❯ ../../test-setup.ts:145:14
      143| );
      144| 
      145| getTestBed().initTestEnvironment(
         |              ^
      146|   BrowserTestingModule,
      147|   platformBrowserTesting(),

Please provide the environment you discovered this bug in (run ng version)

vitest	4.0.18
@vitest/coverage-v8	4.0.18
@analogjs/vitest-angular	2.2.1
@analogjs/vite-plugin-angular	^2.3.1
@angular/core	21.2.1
@nx/vitest	22.5.4
happy-dom	^20.1.0
typescript	5.9.3
@angular/platform-browser	21.2.1
@angular/core	21.2.1 

Anything else?

This issue is reported in the vitest repository, they implied that "Angular forces isolate to be false.", which cause the issue
[https://github.com/vitest-dev/vitest/issues/8944](Regression: 4.0.5 broke Angular tests’ isolation?)

Activity

  1. added this to the needsTriage milestone on Mar 12, 2026
  2. transferred this issue fromangular/angularon Mar 12, 2026
  3. clydin commented on Mar 12, 2026

    @clydin
    Member

    This issue is reported in the vitest repository, they implied that "Angular forces isolate to be false.",

    The integrated Vitest support via the unit-test builder system defaults isolation to false but it can be configured via a Vitest configuration file if needed/preferred.

    However, from the package list above (specifically, @analogjs/vitest-angular), it appears the project may not be using the Angular build system's Vitest integration. If so, this is either an issue with the aforementioned package or a project configuration concern. Please consider opening an issue with AnalogJS if this is the case as it is a third-party package which is not directly supported by the Angular team. If not, please provide the relevant test target within the project's angular.json file.

  4. alan-agius4 commented on Mar 19, 2026

    @alan-agius4
    Collaborator

    Thanks for reporting this issue. However, you didn't provide sufficient information for us to understand and reproduce the problem. Please check out our submission guidelines to understand why we can't act on issues that are lacking important information.

    If the problem persists, please file a new issue and ensure you provide all of the required information when filling out the issue template.

  5. angular-automatic-lock-bot commented on Apr 19, 2026

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  6. locked and limited conversation to collaborators on Apr 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    gemini-triagedLabel noting that an issue has been triaged by geminineeds: more infoReporter must clarify the issue

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions