Skip to content

Commit 5469999

Browse files
committed
refactor(@angular/build): avoid secondary import wrapper in Vitest unless coverage is enabled
This commit optimizes the Vitest test runner by removing the secondary import wrapper for test entry points when code coverage is not enabled. The wrapper is only necessary to support coverage exclusion of the test files themselves. By capturing the resolved Vitest configuration within the `configureVitest` hook, the plugin can now determine if coverage is enabled and conditionally apply the wrapper. This simplifies the module graph for standard test runs.
1 parent f712361 commit 5469999

File tree

1 file changed

+14
-6
lines changed
  • packages/angular/build/src/builders/unit-test/runners/vitest

1 file changed

+14
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import path from 'node:path';
1414
import type {
1515
BrowserConfigOptions,
1616
InlineConfig,
17+
ResolvedConfig,
1718
UserWorkspaceConfig,
1819
VitestPlugin,
1920
} from 'vitest/node';
@@ -185,11 +186,15 @@ async function loadResultFile(file: ResultFile): Promise<string> {
185186
export function createVitestPlugins(pluginOptions: PluginOptions): VitestPlugins {
186187
const { workspaceRoot, buildResultFiles, testFileToEntryPoint } = pluginOptions;
187188
const isWindows = platform() === 'win32';
189+
let vitestConfig: ResolvedConfig;
188190

189191
return [
190192
{
191193
name: 'angular:test-in-memory-provider',
192194
enforce: 'pre',
195+
configureVitest(context) {
196+
vitestConfig = context.vitest.config;
197+
},
193198
resolveId: (id, importer) => {
194199
// Fast path for test entry points.
195200
if (testFileToEntryPoint.has(id)) {
@@ -248,7 +253,7 @@ export function createVitestPlugins(pluginOptions: PluginOptions): VitestPlugins
248253
// If the module cannot be resolved from the build artifacts, let other plugins handle it.
249254
return undefined;
250255
},
251-
load: async (id) => {
256+
async load(id) {
252257
assert(buildResultFiles.size > 0, 'buildResult must be available for in-memory loading.');
253258

254259
// Attempt to load as a source test file.
@@ -257,11 +262,14 @@ export function createVitestPlugins(pluginOptions: PluginOptions): VitestPlugins
257262
if (entryPoint) {
258263
outputPath = entryPoint + '.js';
259264

260-
// To support coverage exclusion of the actual test file, the virtual
261-
// test entry point only references the built and bundled intermediate file.
262-
return {
263-
code: `import "./${outputPath}";`,
264-
};
265+
if (vitestConfig.coverage.enabled) {
266+
// To support coverage exclusion of the actual test file, the virtual
267+
// test entry point only references the built and bundled intermediate file.
268+
// If vitest supported an "excludeOnlyAfterRemap" option, this could be removed completely.
269+
return {
270+
code: `import "./${outputPath}";`,
271+
};
272+
}
265273
} else {
266274
// Attempt to load as a built artifact.
267275
const relativePath = path.relative(workspaceRoot, id);

0 commit comments

Comments
 (0)