Skip to content

Commit 55145f5

Browse files
committed
fix(@angular/build): ensure Vitest setup files are executed in order
This commit configures the Vitest runner to execute setup files in the order they are defined. This is crucial for ensuring that polyfills and the Angular TestBed are initialized in the correct sequence before any tests are run, preventing potential issues with the testing environment. (cherry picked from commit 1ef24a7)
1 parent 931c62d commit 55145f5

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export async function createVitestConfigPlugin(
119119
globals: true,
120120
// Default to `false` to align with the Karma/Jasmine experience.
121121
isolate: false,
122+
sequence: { setupFiles: 'list' },
122123
},
123124
optimizeDeps: {
124125
noDiscovery: true,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import assert from 'node:assert/strict';
2+
import { applyVitestBuilder } from '../../utils/vitest';
3+
import { exec, ng } from '../../utils/process';
4+
import { installPackage } from '../../utils/packages';
5+
import { writeFile } from '../../utils/fs';
6+
7+
export default async function (): Promise<void> {
8+
await applyVitestBuilder();
9+
await installPackage('playwright@1');
10+
await installPackage('@vitest/browser-playwright@4');
11+
await exec('npx', 'playwright', 'install', 'chromium', '--only-shell');
12+
13+
await ng('generate', 'component', 'my-comp');
14+
15+
await writeFile(
16+
'src/setup1.ts',
17+
`
18+
import { getTestBed } from '@angular/core/testing';
19+
20+
getTestBed().configureTestingModule({});
21+
`,
22+
);
23+
24+
const { stdout } = await ng(
25+
'test',
26+
'--no-watch',
27+
'--browsers',
28+
'chromiumHeadless',
29+
'--setup-files',
30+
'src/setup1.ts',
31+
);
32+
33+
assert.match(stdout, /2 passed/, 'Expected 2 tests to pass.');
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import assert from 'node:assert/strict';
2+
import { applyVitestBuilder } from '../../utils/vitest';
3+
import { ng } from '../../utils/process';
4+
import { installPackage } from '../../utils/packages';
5+
import { writeFile } from '../../utils/fs';
6+
7+
export default async function (): Promise<void> {
8+
await applyVitestBuilder();
9+
await installPackage('webdriverio@9');
10+
await installPackage('@vitest/browser-webdriverio@4');
11+
12+
await ng('generate', 'component', 'my-comp');
13+
14+
await writeFile(
15+
'src/setup1.ts',
16+
`
17+
import { getTestBed } from '@angular/core/testing';
18+
19+
getTestBed().configureTestingModule({});
20+
`,
21+
);
22+
23+
const { stdout } = await ng(
24+
'test',
25+
'--no-watch',
26+
'--browsers',
27+
'chromeHeadless',
28+
'--setup-files',
29+
'src/setup1.ts',
30+
);
31+
32+
assert.match(stdout, /2 passed/, 'Expected 2 tests to pass.');
33+
}

0 commit comments

Comments
 (0)