Skip to content

zone.js is not automatically loaded when running unit-test builder for Angular libraries #33477

Description

@Carlosamouco

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 unit tests for an Angular library, zone.js is not automatically imported, unlike when running tests for an Angular application.

This issue occurs when the Angular workspace is explicitly created with zoneless: false.

From what I could determine, the Angular CLI resolves the required polyfills from the polyfills property in angular.json. Since libraries do not have a polyfills configuration, zone.js is never loaded automatically.

As a result, tests that rely on Angular's testing utilities fail unless zone.js is imported manually.

Minimal Reproduction

  1. Create a new Angular workspace with zoneless: false.
  2. Generate a library.
  3. Add a unit test that uses Angular's testing utilities (e.g. TestBed).
  4. Run the library tests.
  5. Observe that zone.js is not loaded unless it is imported manually.

Workaround

To make library tests work correctly, it is necessary to manually configure the test setup by:

  • Importing zone.js in the test setup file
  • Importing and providing provideZoneChangeDetection in the test module configuration

Your Environment

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI       : 22.0.4
Angular           : 22.0.2
Node.js           : 24.16.0
Package Manager   : npm 11.13.0
Operating System  : linux x64

┌───────────────────────────────────┬───────────────────┬───────────────────┐
│ Package                           │ Installed Version │ Requested Version │
├───────────────────────────────────┼───────────────────┼───────────────────┤
│ @angular/animations               │ 22.0.2            │ ^22.0.2           │
│ @angular/build                    │ 22.0.4            │ ^22.0.4           │
│ @angular/cli                      │ 22.0.4            │ ^22.0.4           │
│ @angular/common                   │ 22.0.2            │ ^22.0.2           │
│ @angular/compiler                 │ 22.0.2            │ ^22.0.2           │
│ @angular/compiler-cli             │ 22.0.2            │ ^22.0.2           │
│ @angular/core                     │ 22.0.2            │ ^22.0.2           │
│ @angular/forms                    │ 22.0.2            │ ^22.0.2           │
│ @angular/localize                 │ 22.0.2            │ ^22.0.2           │
│ @angular/platform-browser         │ 22.0.2            │ ^22.0.2           │
│ @angular/platform-browser-dynamic │ 22.0.2            │ ^22.0.2           │
│ @angular/router                   │ 22.0.2            │ ^22.0.2           │
│ @angular/service-worker           │ 22.0.2            │ ^22.0.2           │
│ ng-packagr                        │ 22.0.0            │ ^22.0.0           │
│ rxjs                              │ 7.8.2             │ ~7.8.2            │
│ typescript                        │ 6.0.3             │ ^6.0.3            │
│ vitest                            │ 4.1.9             │ ^4.1.9            │
│ zone.js                           │ 0.16.2            │ ~0.16.2           │
└───────────────────────────────────┴───────────────────┴───────────────────┘

Anything else relevant?

To the best of my knowledge, there is currently no clear indication in the official Angular documentation addressing this specific behaviour or limitation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions