Skip to content

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

@yjaaidi

Description

@yjaaidi

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 using unit test builder + vitest + browser mode, adding the following setup file

import { getTestBed } from '@angular/core/testing';

getTestBed().configureTestingModule({});

causes the following error:

Error: Need to call TestBed.initTestEnvironment() first

After dumping virtual files, I do not see the setup file. I am afraid that there might be a duplicate instance of TestBed, one in the chunk produced by Angular CLI and one directly loaded from node_modules by vitest.

I think that the setup files should be added to the entrypoints here:

entryPoints.set('init-testbed', 'angular:test-bed-init');

It would be also nice to make sure sequence.setupFiles is set to list.

Minimal Reproduction

  • add src/test-setup.ts with:
import { getTestBed } from '@angular/core/testing';

getTestBed().configureTestingModule({});
  • add it to setupFiles option: "setupFiles": ["./src/test-setup.ts"]
  • configure browsers: "browsers": ["ChromiumHeadless"]
  • run tests

Exception or Error

Error: Need to call TestBed.initTestEnvironment() first

Your Environment

Angular CLI       : 21.1.0-next.0+sha-0aab115
Angular           : 21.0.0-rc.1
Node.js           : 24.8.0
Package Manager   : pnpm 10.17.0
Operating System  : darwin arm64

┌───────────────────────────┬───────────────────────────┬───────────────────────────┐
│ Package                   │ Installed Version         │ Requested Version         │
├───────────────────────────┼───────────────────────────┼───────────────────────────┤
│ @angular/build            │ 21.0.0-rc.1               │ ^21.0.0-rc.1              │
│ @angular/cli              │ 21.1.0-next.0+sha-0aab115 │ github:angular/cli-builds │
│ @angular/common           │ 21.0.0-rc.1               │ ^21.0.0-rc.1              │
│ @angular/compiler         │ 21.0.0-rc.1               │ ^21.0.0-rc.1              │
│ @angular/compiler-cli     │ 21.0.0-rc.1               │ ^21.0.0-rc.1              │
│ @angular/core             │ 21.0.0-rc.1               │ ^21.0.0-rc.1              │
│ @angular/forms            │ 21.0.0-rc.1               │ ^21.0.0-rc.1              │
│ @angular/platform-browser │ 21.0.0-rc.1               │ ^21.0.0-rc.1              │
│ @angular/router           │ 21.0.0-rc.1               │ ^21.0.0-rc.1              │
│ rxjs                      │ 7.8.2                     │ ~7.8.0                    │
│ typescript                │ 5.9.3                     │ ~5.9.2                    │
│ vitest                    │ 4.0.8                     │ 4.0.8

Anything else relevant?

Current workaround:

  • Create a custom testing build target with "externalDependencies": ["@angular/core", "@angular/core/testing"].
  • Use it as a buildTarget in the unit-test builder config.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions