question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global $localize() function is not loaded in tests context

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
jarodsmkcommented, Jan 7, 2021

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:

module.exports = {
    name: 'module-report',
    ...
    setupFiles: ['./setup-jest.ts']
};
2reactions
pkauficommented, Feb 11, 2020

I opened an issue (https://github.com/thymikee/jest-preset-angular/issues/347) with an updated repro, let’s see what they think 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 9 introduced a global '$localize()' function that needs ...
Angular 9 introduced a global $localize() function that needs to be loaded. Please add import '@angular/localize'; to your polyfills.ts file.
Read more >
Developers - Global $localize() function is not loaded in tests context -
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
Read more >
$localize Global Import Migration - Angular
This package requires a global $localize symbol to exist. The symbol is loaded by importing the @angular/localize/init module, which has the side effect...
Read more >
Internationalization with @angular/localize | Ninja Squad
Angular 9 introduced a global `$localize()` function that needs to be loaded. Please run `ng add @angular/localize` from the Angular CLI.
Read more >
Globalization and localization in ASP.NET Core
For many developers the new workflow of not having a default language .resx file and simply wrapping the string literals can reduce the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found