Skip to content

vitest: error when a file contains several tests #30186

Closed

Description

@cexbrayat

Command

test

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

When running ng test on a spec file with several tests in the test suite, then the command fails.

Minimal Reproduction

npx @angular/[email protected] new vitest-repro --defaults --no-routing
cd vitest-repro
npm i --save-dev --save-exact vitest jsdom

Replace the test config in angular.json to use vitest:

        "test": {
          "builder": "@angular/build:unit-test",
          "options": {
              "tsConfig": "tsconfig.spec.json",
              "buildTarget": "::development",
              "runner": "vitest"
          }
        }

Update the default app.spec.ts with the appropriate imports at the top:

+ import { beforeEach, describe, expect, it } from 'vitest';

Run the tests with ng test

Exception or Error

❯ spec-app-app.spec.js (3 tests | 2 failed) 79ms
   ✓ App > should create the app 27ms
   × App > should have the 'vitest-repro' title 51ms
     → Cannot configure the test module when the test module has already been instantiated. Make sure you are not using `inject` before `TestBed.configureTestingModule`.
   × App > should render title 0ms
     → Cannot configure the test module when the test module has already been instantiated. Make sure you are not using `inject` before `TestBed.configureTestingModule`.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 2 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  spec-app-app.spec.js > App > should have the 'vitest-repro' title
 FAIL  spec-app-app.spec.js > App > should render title
Error: Cannot configure the test module when the test module has already been instantiated. Make sure you are not using `inject` before `TestBed.configureTestingModule`.
 ❯ _TestBedImpl.assertNotInstantiated node_modules/@angular/core/fesm2022/testing.mjs:2100:19
 ❯ _TestBedImpl.configureTestingModule node_modules/@angular/core/fesm2022/testing.mjs:1978:14
 ❯ Function.configureTestingModule node_modules/@angular/core/fesm2022/testing.mjs:1812:37
 ❯ src/app/app.spec.ts:7:19
 ❯ chunk-GELMMDPB.js:152:61
    151|           }
    152|           const { _finalizers } = this;
    153|           if (_finalizers) {
       |                     ^
    154|             this._finalizers = null;
    155|             for (const finalizer of _finalizers) {
 ❯ new ZoneAwarePromise node_modules/zone.js/fesm2015/zone.js:2702:25
 ❯ __async chunk-GELMMDPB.js:136:10
 ❯ src/app/app.spec.ts:6:25

Your Environment

Angular CLI: 20.0.0-next.8
Node: 22.14.0
Package Manager: npm 10.9.0
OS: darwin arm64

Angular: 20.0.0-next.8
... build, cli, common, compiler, compiler-cli, core, forms
... platform-browser, router

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.2000.0-next.8
@angular-devkit/core         20.0.0-next.8
@angular-devkit/schematics   20.0.0-next.8
@schematics/angular          20.0.0-next.8
rxjs                         7.8.2
typescript                   5.8.3
zone.js                      0.15.0

Anything else relevant?

No response

Activity

  1. added theissue type on Apr 28, 2025
  2. added 2 commits that reference this issue on Apr 28, 2025
    40ce720
    7b6abea
  3. clydin commented on Apr 28, 2025

    @clydin
    Member

    You can also add a TestBed.resetTestingModule to pair with the existing TestBed.configureTestingModule:

      beforeEach(async () => {
        await TestBed.configureTestingModule({
          imports: [App],
        }).compileComponents();
      });
    
      afterEach(() => TestBed.resetTestingModule());
    
  4. added a commit that references this issue on May 2, 2025
    3d54741
  5. added 2 commits that reference this issue on May 2, 2025
    8e0b14f
    1cd65a0
  6. angular-automatic-lock-bot commented on Jun 2, 2025

    @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.

  7. locked and limited conversation to collaborators on Jun 2, 2025
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

    angular/build:unit-testarea: @angular/buildfreq1: lowseverity3: brokentype: bug/fix

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions