Skip to content

Commit e3a2713

Browse files
committed
test(@angular/build): add E2E test for Vitest browser mode with coverage
This test ensures that stack traces map correctly to source files and that coverage reports are generated when running Vitest in browser mode with coverage enabled. This provides validation for the current implementation and will help verify future refactors removing the source-map-support dependency.
1 parent 5adc925 commit e3a2713

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import assert from 'node:assert/strict';
2+
import { applyVitestBuilder } from '../../utils/vitest';
3+
import { ng, noSilentNg } from '../../utils/process';
4+
import { installPackage } from '../../utils/packages';
5+
import { expectFileToExist, readFile, writeFile } from '../../utils/fs';
6+
import { stripVTControlCharacters } from 'node:util';
7+
8+
export default async function (): Promise<void> {
9+
await applyVitestBuilder();
10+
await installPackage('playwright@1');
11+
await installPackage('@vitest/browser-playwright@4');
12+
await installPackage('@vitest/coverage-v8@4');
13+
await ng('generate', 'component', 'my-comp');
14+
15+
// Add a failing test to verify source map support
16+
await writeFile(
17+
'src/app/failing.spec.ts',
18+
`
19+
describe('Failing Test', () => {
20+
it('should fail', () => {
21+
expect(true).toBe(false);
22+
});
23+
});
24+
`,
25+
);
26+
27+
try {
28+
await noSilentNg('test', '--no-watch', '--browsers', 'chromiumHeadless', '--coverage');
29+
throw new Error('Expected "ng test" to fail.');
30+
} catch (error: any) {
31+
const stdout = stripVTControlCharacters(error.stdout || error.message);
32+
console.log('STDOUT: ', stdout);
33+
// We expect the failure from failing.spec.ts
34+
assert.match(stdout, /1 failed/, 'Expected 1 test to fail.');
35+
// Check that the stack trace points to the correct file
36+
assert.match(
37+
stdout,
38+
/\\bsrc[\\/\\\\]app[\\/\\\\]failing\\.spec\\.ts:4:\\d+/,
39+
'Expected stack trace to point to the source file.',
40+
);
41+
}
42+
43+
// Verify that coverage files are generated
44+
const coverageJsonPath = 'coverage/test-project/coverage-final.json';
45+
await expectFileToExist(coverageJsonPath);
46+
47+
const coverageContent = await readFile(coverageJsonPath);
48+
assert.match(
49+
coverageContent,
50+
/app\.component\.ts/,
51+
'Expected coverage report to contain app.component.ts.',
52+
);
53+
}

0 commit comments

Comments
 (0)