Skip to content

Commit 4ce5602

Browse files
committed
test: add e2e test for Vitest browser configuration with Vitest v4
This commits adds a test to validate vitest version 4.
1 parent 438bc08 commit 4ce5602

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import assert from 'node:assert/strict';
2+
import { applyVitestBuilder } from '../../utils/vitest';
3+
import { ng } from '../../utils/process';
4+
import { writeFile } from '../../utils/fs';
5+
import { installWorkspacePackages } from '../../utils/packages';
6+
import { updateJsonFile } from '../../utils/project';
7+
8+
export default async function (): Promise<void> {
9+
await applyVitestBuilder({
10+
skipInstall: true,
11+
});
12+
13+
await updateJsonFile('package.json', (json) => {
14+
json.devDependencies = {
15+
...json.devDependencies,
16+
'@vitest/browser-playwright': '^4',
17+
'vitest': '^4',
18+
'playwright': '^1',
19+
};
20+
});
21+
22+
await installWorkspacePackages();
23+
24+
await ng('generate', 'component', 'my-comp');
25+
26+
// Create vitest-base.config.mts
27+
await writeFile(
28+
'vitest-base.config.mts',
29+
`
30+
import { defineConfig } from 'vitest/config';
31+
import { playwright } from '@vitest/browser-playwright';
32+
33+
export default defineConfig({
34+
test: {
35+
browser: {
36+
enabled: true,
37+
provider: playwright({launchOptions: {executablePath: process.env['CHROME_BIN']}}),
38+
instances: [
39+
{ browser: 'chromium' },
40+
],
41+
headless: true,
42+
},
43+
},
44+
});
45+
`,
46+
);
47+
48+
// Create a spec file that asserts browser environment
49+
await writeFile(
50+
'src/browser.spec.ts',
51+
`
52+
describe('Browser Environment Check', () => {
53+
it('should be running in Chromium', () => {
54+
const ua = navigator.userAgent;
55+
// Fail the test if it's not Chrome/Chromium
56+
if (!ua.includes('Chrome') && !ua.includes('Chromium')) {
57+
throw new Error('Expected to be running in Chrome/Chromium, but User Agent is: ' + ua);
58+
}
59+
});
60+
});
61+
`,
62+
);
63+
64+
const { stdout } = await ng('test', '--no-watch', '--runner-config');
65+
66+
assert.match(stdout, /3 passed/, 'Expected 3 tests to pass.');
67+
}

tests/e2e/utils/vitest.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ export async function applyVitestBuilder(options?: {
1010
coverageIstanbul?: boolean;
1111
playwright?: boolean;
1212
webdriver?: boolean;
13+
skipInstall?: boolean;
1314
}): Promise<void> {
14-
const { coverageV8, coverageIstanbul, playwright, webdriver } = options ?? {};
15+
const { coverageV8, coverageIstanbul, playwright, webdriver, skipInstall } = options ?? {};
1516

1617
const schematicsAngular = createRequire(process.cwd() + '/').resolve(
1718
'@schematics/angular/package.json',
@@ -47,7 +48,9 @@ export async function applyVitestBuilder(options?: {
4748
}
4849
});
4950

50-
await installWorkspacePackages();
51+
if (!skipInstall) {
52+
await installWorkspacePackages();
53+
}
5154

5255
await updateJsonFile('angular.json', (json) => {
5356
const projects = Object.values(json['projects']);

0 commit comments

Comments
 (0)