Skip to content

vitest: error when a file contains several tests #30186

@cexbrayat

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

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions