Command
test
Is this a regression?
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
-
Create a workspace with two libraries:
-
Create one component in each library.
-
In the base-lib component, define a property using $localize:
title = $localize`Base lib component title.`;
-
In the feature-lib component, import and use the component from base-lib.
-
Run:
- The test execution fails with:
ReferenceError: $localize is not defined
Additional scenario
- Define a
providersFile for feature-lib.
- Import a service from
base-lib in the providersFile.
- Run:
- 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
Command
test
Is this a regression?
The previous version in which this bug was not present was
No response
Description
Unit tests fail with
ReferenceError: $localize is not definedwhen 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:
However, this produces the following warning during the test build:
A similar issue occurs when using a
providersFilethat imports a service from the dependent library. In this scenario, the sameReferenceError: $localize is not definedis thrown while evaluating the imported service.Unlike the component import scenario, importing
@angular/localize/initin theprovidersFiledoes not resolve the issue, and the test run still fails.Expected behavior:
$localizeshould be available when executing tests that consume code from local library dependencies, including code imported through aprovidersFile.Actual behavior:
ReferenceError: $localize is not definedwhen evaluating code imported from a dependent library.@angular/localize/initworks as a workaround in some scenarios but emits a warning.providersFile, importing@angular/localize/initin that file does not resolve the issue.Minimal Reproduction
Steps to reproduce
Create a workspace with two libraries:
base-libfeature-libCreate one component in each library.
In the
base-libcomponent, define a property using$localize:In the
feature-libcomponent, import and use the component frombase-lib.Run:
ng test feature-libAdditional scenario
providersFileforfeature-lib.base-libin theprovidersFile.ng test feature-libImporting
@angular/localize/initin theprovidersFiledoes not resolve the issue.Exception or Error
Your Environment
Anything else relevant?
No response