Global $localize() function is not loaded in tests context
See original GitHub issueDescribe the Bug
After running ngcc
as proposed in the angular update guide (https://update.angular.io/#8.2:9.0), tests are failing with the error message
It looks like your application or one of its dependencies is using i18n.
Angular 9 introduced a global `$localize()` function that needs to be loaded.
Please run `ng add @angular/localize` from the Angular CLI.
(For non-CLI projects, add `import '@angular/localize/init';` to your `polyfills.ts` file.
For server-side rendering applications add the import to your `main.server.ts` file.)
I added a small reproduction below (updated and adapted the example app in this repository), where I installed @angular/localized
with the help of the cli (ng add @angular/localized
). It then added an import to the polyfills.ts
files automatically (polyfills.ts).
It looks like polyfills.ts
is not properly picked up during the test run, as ng serve
still works after running ngcc
.
When you add a minimal jest.config.js
inside the root directory looking like
module.exports = {
setupFilesAfterEnv: '<rootDir>/setupJest.ts',
};
and the corresponding setupJest.ts
file
import '@angular/localize/init';
the tests are green again. In my production app, this still breaks some other tests, but at least the majority of them runs. However, I don’t think that’s the proper solution for it.
Minimal Reproduction
https://github.com/pkaufi/ngcc-i18n-jest
Expected Behavior
Tests should run successfully after running ngcc
Environment
see reproduction repository
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:9 (4 by maintainers)
Top GitHub Comments
The issue was resolved within https://github.com/thymikee/jest-preset-angular/issues/347 -
Simply add the following to your setup-jest.ts:
import '@angular/localize/init';
And make sure it’s imported into your jest.config.js:
I opened an issue (https://github.com/thymikee/jest-preset-angular/issues/347) with an updated repro, let’s see what they think 😃