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

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