Skip to content

Tests using @angular-builders/jest cannot import jest object from @jest/globals #28722

Open
@Tommy228

Description

@Tommy228

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

Importing jest from @jest/globals with the new test builder fails with the error Do not import @jest/globals outside of the Jest test environment.

This isn't an issue when writing spec files as you can directly access it (probably thanks to this) or use import.meta.jest, however this breaks compatibility with third party libs such as Spectator.

Minimal Reproduction

import { jest } from '@jest/globals';

describe('test', () => {
  beforeEach(() => {
    jest.useFakeTimers();
  });
});

Exception or Error

 FAIL  dist/test-out/4c1de1b8-6e3e-4b2a-add2-968166a278d8/app.component.spec.mjs
  ● Test suite failed to run

    Do not import `@jest/globals` outside of the Jest test environment

      at node_modules/@jest/globals/build/index.js:11:7

Your Environment

Angular CLI: 18.2.9
Node: 22.10.0
Package Manager: npm 10.9.0
OS: win32 x64

Angular: 18.2.8
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1802.9
@angular-devkit/build-angular   18.2.9
@angular-devkit/core            18.2.9
@angular-devkit/schematics      18.2.9
@angular/cli                    18.2.9
@schematics/angular             18.2.9
rxjs                            7.8.1
typescript                      5.5.4
zone.js                         0.14.10

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