Skip to content

unit-test builder with vitest fails to properly handle $localize call's in library projects #33450

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

Unit tests fail with ReferenceError: $localize is not defined when a library imports code from another local library that uses $localize.

The error occurs during the evaluation of the imported dependency, causing the test run to fail before any tests are executed.

As a workaround, it is possible to explicitly import:

import '@angular/localize/init';

However, this produces the following warning during the test build:

▲ [WARNING] Direct import of '@angular/localize/init' detected. This may lead to undefined behavior. [plugin angular-localize-init-warning]

A similar issue occurs when using a providersFile that imports a service from the dependent library. In this scenario, the same ReferenceError: $localize is not defined is thrown while evaluating the imported service.

Unlike the component import scenario, importing @angular/localize/init in the providersFile does not resolve the issue, and the test run still fails.

Expected behavior:

  • $localize should be available when executing tests that consume code from local library dependencies, including code imported through a providersFile.
  • No additional setup should be required to initialize the localization runtime.

Actual behavior:

  • Tests fail with ReferenceError: $localize is not defined when evaluating code imported from a dependent library.
  • Importing @angular/localize/init works as a workaround in some scenarios but emits a warning.
  • When the dependency is imported through a providersFile, importing @angular/localize/init in that file does not resolve the issue.

Minimal Reproduction

Steps to reproduce

  1. Create a workspace with two libraries:

    • base-lib
    • feature-lib
  2. Create one component in each library.

  3. In the base-lib component, define a property using $localize:

title = $localize`Base lib component title.`;
  1. In the feature-lib component, import and use the component from base-lib.

  2. Run:

ng test feature-lib
  1. The test execution fails with:
ReferenceError: $localize is not defined

Additional scenario

  1. Define a providersFile for feature-lib.
  2. Import a service from base-lib in the providersFile.
  3. Run:
ng test feature-lib
  1. The test execution fails with the same:
ReferenceError: $localize is not defined

Importing @angular/localize/init in the providersFile does not resolve the issue.

Exception or Error


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/build            │ 22.0.4            │ ^22.0.4           │
│ @angular/cli              │ 22.0.4            │ ^22.0.4           │
│ @angular/common           │ 22.0.2            │ ^22.0.0           │
│ @angular/compiler         │ 22.0.2            │ ^22.0.0           │
│ @angular/compiler-cli     │ 22.0.2            │ ^22.0.0           │
│ @angular/core             │ 22.0.2            │ ^22.0.0           │
│ @angular/forms            │ 22.0.2            │ ^22.0.0           │
│ @angular/localize         │ 22.0.2            │ ^22.0.2           │
│ @angular/platform-browser │ 22.0.2            │ ^22.0.0           │
│ @angular/router           │ 22.0.2            │ ^22.0.0           │
│ ng-packagr                │ 22.0.0            │ ^22.0.0           │
│ rxjs                      │ 7.8.2             │ ~7.8.0            │
│ typescript                │ 6.0.3             │ ~6.0.2            │
│ vitest                    │ 4.1.9             │ ^4.0.8            │
└───────────────────────────┴───────────────────┴──────────────────

Anything else relevant?

No response

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