Skip to content

Commit 566de64

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular/build): use virtual module for Karma TestBed initialization
Using a virtual module for the Karma-based unit testing setup allows for the Angular packages used within the TestBed initialization to resolve to the same dependencies used within the application and test code. This avoids the potential for multiple copies of `@angular/core` within the test output which can result in TestBed execution failure due to the module level variable usage within TestBed.
1 parent 32d5476 commit 566de64

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

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

+24-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { ReadableStreamController } from 'node:stream/web';
1717
import { globSync } from 'tinyglobby';
1818
import { BuildOutputFileType } from '../../tools/esbuild/bundler-context';
1919
import { emitFilesToDisk } from '../../tools/esbuild/utils';
20+
import { createVirtualModulePlugin } from '../../tools/esbuild/virtual-module-plugin';
2021
import { buildApplicationInternal } from '../application/index';
2122
import { ApplicationBuilderInternalOptions } from '../application/options';
2223
import { Result, ResultFile, ResultKind } from '../application/results';
@@ -383,7 +384,7 @@ async function initializeApplication(
383384
if (options.main) {
384385
entryPoints.set(mainName, options.main);
385386
} else {
386-
entryPoints.set(mainName, localResolve('./polyfills/init_test_bed.js'));
387+
entryPoints.set(mainName, 'angular:test-bed-init');
387388
}
388389

389390
const instrumentForCoverage = options.codeCoverage
@@ -429,9 +430,30 @@ async function initializeApplication(
429430
externalDependencies: options.externalDependencies,
430431
};
431432

433+
const virtualTestBedInit = createVirtualModulePlugin({
434+
namespace: 'angular:test-bed-init',
435+
loadContent: async () => {
436+
const contents: string[] = [
437+
// Initialize the Angular testing environment
438+
`import { getTestBed } from '@angular/core/testing';`,
439+
`import { BrowserTestingModule, platformBrowserTesting } from '@angular/platform-browser/testing';`,
440+
`getTestBed().initTestEnvironment(BrowserTestingModule, platformBrowserTesting(), {`,
441+
` errorOnUnknownElements: true,`,
442+
` errorOnUnknownProperties: true,`,
443+
'});',
444+
];
445+
446+
return {
447+
contents: contents.join('\n'),
448+
loader: 'js',
449+
resolveDir: projectSourceRoot,
450+
};
451+
},
452+
});
453+
432454
// Build tests with `application` builder, using test files as entry points.
433455
const [buildOutput, buildIterator] = await first(
434-
buildApplicationInternal(buildOptions, context),
456+
buildApplicationInternal(buildOptions, context, { codePlugins: [virtualTestBedInit] }),
435457
{ cancel: !buildOptions.watch },
436458
);
437459
if (buildOutput.kind === ResultKind.Failure) {

packages/angular/build/src/builders/karma/polyfills/init_test_bed.js

-16
This file was deleted.

0 commit comments

Comments
 (0)