Skip to content

Commit f074f13

Browse files
committed
test: improve vitest e2e test stability and performance
- Use Chrome binary from `rules_browsers` for Playwright in Vitest browser tests for hermetic testing. - Batch `ng generate` commands in e2e tests to improve performance. - Remove now redundant chromium install commands from e2e tests.
1 parent c241dd8 commit f074f13

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,21 @@ export async function setupBrowserConfiguration(
7676
// Validate that the imported module has the expected structure
7777
const providerFactory = providerModule[providerName];
7878
if (typeof providerFactory === 'function') {
79-
provider = providerFactory();
79+
if (
80+
providerName === 'playwright' &&
81+
process.env['CHROME_BIN']?.includes('rules_browsers')
82+
) {
83+
// Use the Chrome binary from the 'rules_browsers' toolchain (via CHROME_BIN)
84+
// for Playwright when available to ensure hermetic testing, preventing reliance
85+
// on locally installed or NPM-managed browser versions.
86+
provider = providerFactory({
87+
launchOptions: {
88+
executablePath: process.env.CHROME_BIN,
89+
},
90+
});
91+
} else {
92+
provider = providerFactory();
93+
}
8094
} else {
8195
errors ??= [];
8296
errors.push(

tests/legacy-cli/e2e/tests/vitest/larger-project.ts

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,7 @@ export default async function () {
1212
// Each generated artifact will add one more test file.
1313
const initialTestCount = 1;
1414

15-
// Generate a mix of components, services, and pipes
16-
for (let i = 0; i < artifactCount; i++) {
17-
const type = i % 3;
18-
const name = `test-artifact-${i}`;
19-
let generateType;
20-
21-
switch (type) {
22-
case 0:
23-
generateType = 'component';
24-
break;
25-
case 1:
26-
generateType = 'service';
27-
break;
28-
default:
29-
generateType = 'pipe';
30-
break;
31-
}
32-
33-
await ng('generate', generateType, name, '--skip-tests=false');
34-
}
15+
await generateArtifactsInBatches(artifactCount);
3516

3617
const totalTests = initialTestCount + artifactCount;
3718
const expectedMessage = new RegExp(`${totalTests} passed`);
@@ -43,7 +24,6 @@ export default async function () {
4324
// Setup for browser mode
4425
await installPackage('playwright@1');
4526
await installPackage('@vitest/browser-playwright@4');
46-
await exec('npx', 'playwright', 'install', 'chromium', '--only-shell');
4727

4828
// Run tests in browser mode
4929
const { stdout: browserStdout } = await ng(
@@ -58,3 +38,33 @@ export default async function () {
5838
`Expected ${totalTests} tests to pass in browser mode.`,
5939
);
6040
}
41+
42+
async function generateArtifactsInBatches(artifactCount: number): Promise<void> {
43+
const BATCH_SIZE = 5;
44+
let commands: Promise<any>[] = [];
45+
46+
for (let i = 0; i < artifactCount; i++) {
47+
const type = i % 3;
48+
const name = `test-artifact-${i}`;
49+
let generateType: string;
50+
51+
switch (type) {
52+
case 0:
53+
generateType = 'component';
54+
break;
55+
case 1:
56+
generateType = 'service';
57+
break;
58+
default:
59+
generateType = 'pipe';
60+
break;
61+
}
62+
63+
commands.push(ng('generate', generateType, name, '--skip-tests=false'));
64+
65+
if (commands.length === BATCH_SIZE || i === artifactCount - 1) {
66+
await Promise.all(commands);
67+
commands = [];
68+
}
69+
}
70+
}

tests/legacy-cli/e2e/tests/vitest/tslib-resolution.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { writeFile } from '../../utils/fs';
22
import { installPackage } from '../../utils/packages';
3-
import { exec, ng } from '../../utils/process';
3+
import { ng } from '../../utils/process';
44
import { applyVitestBuilder } from '../../utils/vitest';
55
import assert from 'node:assert';
66

77
export default async function () {
88
await applyVitestBuilder();
99
await installPackage('playwright@1');
1010
await installPackage('@vitest/browser-playwright@4');
11-
await exec('npx', 'playwright', 'install', 'chromium', '--only-shell');
1211

1312
// Add a custom decorator to trigger tslib usage
1413
await writeFile(

0 commit comments

Comments
 (0)