Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 75c1dce

Browse filesBrowse files
clydinalan-agius4
authored andcommitted
fix(@angular/build): auto-inject localize/init in library unit tests
Injects '@angular/localize/init' at the top of the internal virtual initialization file (angular:test-bed-init) if '@angular/localize' is installed in the workspace. This prevents ReferenceErrors when testing libraries that consume localization APIs. Also updates the warning plugin to skip internal imports originating from the 'angular:' namespace.
1 parent dc27e17 commit 75c1dce
Copy full SHA for 75c1dce

3 files changed

+23Lines changed: 23 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/angular/build/src/builders/karma/application_builder.ts‎

Copy file name to clipboardExpand all lines: packages/angular/build/src/builders/karma/application_builder.ts
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { Config, ConfigOptions, FilePattern, InlinePluginDef, Server } from
1111
import { randomUUID } from 'node:crypto';
1212
import { rmSync } from 'node:fs';
1313
import * as fs from 'node:fs/promises';
14+
import { createRequire } from 'node:module';
1415
import path from 'node:path';
1516
import { ReadableStream } from 'node:stream/web';
1617
import { createVirtualModulePlugin } from '../../tools/esbuild/virtual-module-plugin';
@@ -199,10 +200,18 @@ async function runEsbuild(
199200
projectSourceRoot: string,
200201
): Promise<[Result & { kind: ResultKind.Full }, AsyncIterator<Result> | null]> {
201202
const usesZoneJS = buildOptions.polyfills?.includes('zone.js');
203+
let hasLocalize = false;
204+
try {
205+
const projectRequire = createRequire(path.join(projectSourceRoot, 'package.json'));
206+
projectRequire.resolve('@angular/localize');
207+
hasLocalize = true;
208+
} catch {}
209+
202210
const virtualTestBedInit = createVirtualModulePlugin({
203211
namespace: 'angular:test-bed-init',
204212
loadContent: async () => {
205213
const contents: string[] = [
214+
...(hasLocalize ? [`import '@angular/localize/init';`] : []),
206215
// Initialize the Angular testing environment
207216
`import { NgModule${usesZoneJS ? ', provideZoneChangeDetection' : ''} } from '@angular/core';`,
208217
`import { getTestBed } from '@angular/core/testing';`,
Collapse file

‎packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts‎

Copy file name to clipboardExpand all lines: packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function createTestBedInitVirtualFile(
3434
projectSourceRoot: string,
3535
teardown: boolean,
3636
zoneTestingStrategy: 'none' | 'static' | 'dynamic',
37+
hasLocalize: boolean,
3738
): string {
3839
let providersImport = 'const providers = [];';
3940
if (providersFile) {
@@ -58,6 +59,7 @@ function createTestBedInitVirtualFile(
5859
// when running Vitest in non-isolated mode with JSDOM. It looks up the
5960
// document dynamically on every operation instead of caching it.
6061
return `
62+
${hasLocalize ? "import '@angular/localize/init';" : ''}
6163
// Initialize the Angular testing environment
6264
import { NgModule, provideZoneChangeDetection } from '@angular/core';
6365
import { getTestBed, ɵgetCleanupHook as getCleanupHook, TestComponentRenderer } from '@angular/core/testing';
@@ -254,11 +256,19 @@ export async function getVitestBuildOptions(
254256
// Inject the zone.js testing polyfill if Zone.js is installed.
255257
const zoneTestingStrategy = getZoneTestingStrategy(buildOptions, projectSourceRoot);
256258

259+
let hasLocalize = false;
260+
try {
261+
const projectRequire = createRequire(path.join(projectSourceRoot, 'package.json'));
262+
projectRequire.resolve('@angular/localize');
263+
hasLocalize = true;
264+
} catch {}
265+
257266
const testBedInitContents = createTestBedInitVirtualFile(
258267
providersFile,
259268
projectSourceRoot,
260269
!options.debug,
261270
zoneTestingStrategy,
271+
hasLocalize,
262272
);
263273

264274
const mockPatchContents = `
Collapse file

‎packages/angular/build/src/tools/esbuild/angular-localize-init-warning-plugin.ts‎

Copy file name to clipboardExpand all lines: packages/angular/build/src/tools/esbuild/angular-localize-init-warning-plugin.ts
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export function createAngularLocalizeInitWarningPlugin(): Plugin {
2626
return null;
2727
}
2828

29+
if (args.namespace?.startsWith('angular:')) {
30+
return null;
31+
}
32+
2933
const { importer, kind, resolveDir, namespace, pluginData = {} } = args;
3034
pluginData[NG_LOCALIZE_RESOLUTION] = true;
3135

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.