Skip to content

Commit 39b307d

Browse files
committed
refactor(@angular/build): remove unnecessary source-map-support polyfill for Vitest browser coverage
Removes the injection of the source-map-support library in the Vitest builder. This workaround was previously required for correct stack traces in browser mode when coverage was enabled but is no longer necessary. This change simplifies the plugin logic and ensures source maps are consistently handled.
1 parent b33678e commit 39b307d

File tree

2 files changed

+17
-40
lines changed

2 files changed

+17
-40
lines changed

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

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,6 @@ export async function createVitestConfigPlugin(
114114
delete config.plugins;
115115
}
116116

117-
// Add browser source map support if coverage is enabled
118-
if (
119-
(browser || testConfig?.browser?.enabled) &&
120-
(options.coverage.enabled || testConfig?.coverage?.enabled)
121-
) {
122-
projectPlugins.unshift(createSourcemapSupportPlugin());
123-
setupFiles.unshift('virtual:source-map-support');
124-
}
125-
126117
const projectResolver = createRequire(projectSourceRoot + '/').resolve;
127118

128119
const projectDefaults: UserWorkspaceConfig = {
@@ -276,7 +267,7 @@ export function createVitestPlugins(pluginOptions: PluginOptions): VitestPlugins
276267

277268
const map = sourceMapText ? JSON.parse(sourceMapText) : undefined;
278269
if (map) {
279-
adjustSourcemapSources(map, !vitestConfig?.coverage?.enabled, workspaceRoot, id);
270+
adjustSourcemapSources(map, true, workspaceRoot, id);
280271
}
281272

282273
return {
@@ -343,36 +334,6 @@ function adjustSourcemapSources(
343334
}
344335
}
345336

346-
function createSourcemapSupportPlugin(): VitestPlugins[0] {
347-
return {
348-
name: 'angular:source-map-support',
349-
enforce: 'pre',
350-
resolveId(source) {
351-
if (source.includes('virtual:source-map-support')) {
352-
return '\0source-map-support';
353-
}
354-
},
355-
async load(id) {
356-
if (id !== '\0source-map-support') {
357-
return;
358-
}
359-
360-
const packageResolve = createRequire(__filename).resolve;
361-
const supportPath = packageResolve('source-map-support/browser-source-map-support.js');
362-
363-
const content = await readFile(supportPath, 'utf-8');
364-
365-
// The `source-map-support` library currently relies on `this` being defined in the global scope.
366-
// However, when running in an ESM environment, `this` is undefined.
367-
// To workaround this, we patch the library to use `globalThis` instead of `this`.
368-
return (
369-
content.replaceAll(/this\.(define|sourceMapSupport|base64js)/g, 'globalThis.$1') +
370-
'\n;globalThis.sourceMapSupport.install();'
371-
);
372-
},
373-
};
374-
}
375-
376337
async function generateCoverageOption(
377338
optionsCoverage: NormalizedUnitTestBuilderOptions['coverage'],
378339
configCoverage: VitestCoverageOption | undefined,

tests/e2e/tests/vitest/browser-sourcemaps.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,20 @@ export default async function (): Promise<void> {
3737
'Expected stack trace to point to the source file.',
3838
);
3939
}
40+
41+
// Again but with coverage
42+
try {
43+
await noSilentNg('test', '--no-watch', '--coverage', '--browsers', 'chromiumHeadless');
44+
throw new Error('Expected "ng test" to fail.');
45+
} catch (error: any) {
46+
const stdout = stripVTControlCharacters(error.stdout || error.message);
47+
// We expect the failure from failing.spec.ts
48+
assert.match(stdout, /1 failed/, 'Expected 1 test to fail.');
49+
// Check that the stack trace points to the correct file
50+
assert.match(
51+
stdout,
52+
/\bsrc[\/\\]app[\/\\]failing\.spec\.ts:4:\d+/,
53+
'Expected stack trace to point to the source file.',
54+
);
55+
}
4056
}

0 commit comments

Comments
 (0)