Skip to content

Commit ce95753

Browse files
clydindgp1130
authored andcommitted
refactor(@angular/build): remove source-map-support from Vitest runner
This change removes the injection of `source-map-support` in Vitest browser tests and enables sourcemap rebasing for coverage runs as well. This allows Vitest's native remapper to handle stack traces and coverage correctly without needing the external polyfill in the browser. The E2E tests have been verified to pass with these changes. The unused `createSourcemapSupportPlugin` function has also been removed.
1 parent 7b41693 commit ce95753

2 files changed

Lines changed: 25 additions & 47 deletions

File tree

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

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,12 @@ export async function createVitestConfigPlugin(
233233
delete config.plugins;
234234
}
235235

236-
// Add browser source map support if coverage is enabled
236+
// Validate browser coverage support if coverage is enabled
237237
if (
238238
(browser || testConfig?.browser?.enabled) &&
239239
(options.coverage.enabled || testConfig?.coverage?.enabled)
240240
) {
241-
// Validate that enabled browsers support the selected coverage provider
242241
validateBrowserCoverage(browser, testConfig?.browser, determinedProvider);
243-
244-
projectPlugins.unshift(createSourcemapSupportPlugin());
245-
setupFiles.unshift('virtual:source-map-support');
246242
}
247243

248244
const projectResolver = createRequire(projectSourceRoot + '/').resolve;
@@ -411,7 +407,7 @@ export function createVitestPlugins(pluginOptions: PluginOptions): VitestPlugins
411407

412408
const map = sourceMapText ? JSON.parse(sourceMapText) : undefined;
413409
if (map) {
414-
adjustSourcemapSources(map, !vitestConfig?.coverage?.enabled, workspaceRoot, id);
410+
adjustSourcemapSources(map, true, workspaceRoot, id);
415411
}
416412

417413
return {
@@ -478,36 +474,6 @@ function adjustSourcemapSources(
478474
}
479475
}
480476

481-
function createSourcemapSupportPlugin(): VitestPlugins[0] {
482-
return {
483-
name: 'angular:source-map-support',
484-
enforce: 'pre',
485-
resolveId(source) {
486-
if (source.includes('virtual:source-map-support')) {
487-
return '\0source-map-support';
488-
}
489-
},
490-
async load(id) {
491-
if (id !== '\0source-map-support') {
492-
return;
493-
}
494-
495-
const packageResolve = createRequire(__filename).resolve;
496-
const supportPath = packageResolve('source-map-support/browser-source-map-support.js');
497-
498-
const content = await readFile(supportPath, 'utf-8');
499-
500-
// The `source-map-support` library currently relies on `this` being defined in the global scope.
501-
// However, when running in an ESM environment, `this` is undefined.
502-
// To workaround this, we patch the library to use `globalThis` instead of `this`.
503-
return (
504-
content.replaceAll(/this\.(define|sourceMapSupport|base64js)/g, 'globalThis.$1') +
505-
'\n;globalThis.sourceMapSupport.install();'
506-
);
507-
},
508-
};
509-
}
510-
511477
interface CustomBrowserConfigOptions {
512478
enabled?: boolean;
513479
instances?: { browser: string }[];

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,32 @@ export default async function (): Promise<void> {
1212

1313
// Run tests with coverage in browser mode.
1414
// We use the default passing tests generated for the project.
15-
const { stdout } = await ng('test', '--no-watch', '--browsers', 'chromiumHeadless', '--coverage');
15+
const { stdout } = await ng(
16+
'test',
17+
'--no-watch',
18+
'--browsers',
19+
'chromiumHeadless',
20+
'--coverage',
21+
'--coverage-reporters=json-summary',
22+
);
1623

1724
// Verify that tests passed
1825
assert.match(stdout, /pass/, 'Expected tests to run successfully.');
1926

2027
// Verify that coverage files are generated
21-
const coverageJsonPath = 'coverage/test-project/coverage-final.json';
22-
await expectFileToExist(coverageJsonPath);
23-
24-
const coverageContent = await readFile(coverageJsonPath);
25-
assert.match(coverageContent, /app\.ts/, 'Expected coverage report to contain app.ts.');
26-
assert.doesNotMatch(
27-
coverageContent,
28-
/\.spec\.ts/,
29-
'Expected coverage report to not contain .spec.ts files.',
30-
);
28+
const summaryPath = 'coverage/test-project/coverage-summary.json';
29+
await expectFileToExist(summaryPath);
30+
31+
const summary = JSON.parse(await readFile(summaryPath));
32+
33+
// Find the key for app.ts (it might be an absolute path)
34+
const appFileKey = Object.keys(summary).find((key) => key.endsWith('app.ts'));
35+
assert.ok(appFileKey, 'Expected coverage summary to contain app.ts.');
36+
37+
const appCoverage = summary[appFileKey];
38+
assert.ok(appCoverage.lines.pct > 0, 'Expected lines percentage to be greater than 0.');
39+
40+
// Also verify that spec files are NOT present in the summary
41+
const specFileKey = Object.keys(summary).find((key) => key.endsWith('.spec.ts'));
42+
assert.ok(!specFileKey, 'Expected coverage report to not contain .spec.ts files.');
3143
}

0 commit comments

Comments
 (0)