Skip to content

Commit fea1f13

Browse files
committed
test: attempt to workaround Windows snapshot E2E flakes
1 parent 138649e commit fea1f13

File tree

3 files changed

+45
-24
lines changed
  • .github/workflows
  • packages/angular/build/src/builders/unit-test/runners/vitest
  • tests/legacy-cli/e2e/tests/vitest

3 files changed

+45
-24
lines changed

.github/workflows/pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ jobs:
171171
test_target_name: e2e.esbuild_node22
172172
env:
173173
E2E_SHARD_TOTAL: 1
174-
TESTBRIDGE_TEST_ONLY: tests/basic/{build,rebuild}.ts
174+
TESTBRIDGE_TEST_ONLY: tests/vitest/snapshot.ts
175175

176176
e2e-package-managers:
177177
needs: build

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export async function createVitestConfigPlugin(
118118
setupFiles,
119119
globals: true,
120120
// Allow Vitest to manage test isolation by its default behavior.
121-
sequence: { setupFiles: 'list' },
121+
// sequence: { setupFiles: 'list' },
122122
},
123123
optimizeDeps: {
124124
noDiscovery: true,

tests/legacy-cli/e2e/tests/vitest/snapshot.ts

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,74 @@
1-
import { ng } from '../../utils/process';
2-
import { appendToFile, replaceInFile, readFile } from '../../utils/fs';
1+
import { exec, ng, noSilentNg } from '../../utils/process';
2+
import { appendToFile, replaceInFile, readFile, writeFile } from '../../utils/fs';
33
import { applyVitestBuilder } from '../../utils/vitest';
44
import assert from 'node:assert/strict';
55
import { stripVTControlCharacters } from 'node:util';
6+
import { setTimeout } from 'node:timers/promises';
67

78
export default async function () {
89
// Set up the test project to use the vitest runner
910
await applyVitestBuilder();
1011

1112
// Add snapshot assertions to the test file
12-
await appendToFile(
13+
await replaceInFile(
1314
'src/app/app.spec.ts',
15+
`describe('App', () => {`,
1416
`
15-
it('should match file snapshot', () => {
16-
const fixture = TestBed.createComponent(App);
17-
const app = fixture.componentInstance;
18-
expect((app as any).title()).toMatchSnapshot();
19-
});
17+
describe('App', () => {
18+
it('should match file snapshot', () => {
19+
const fixture = TestBed.createComponent(App);
20+
const app = fixture.componentInstance;
21+
expect((app as any).title()).toMatchSnapshot();
22+
});
2023
21-
it('should match inline snapshot', () => {
22-
const fixture = TestBed.createComponent(App);
23-
const app = fixture.componentInstance;
24-
expect((app as any).title()).toMatchInlineSnapshot();
25-
});
26-
`,
24+
it('should match inline snapshot', () => {
25+
const fixture = TestBed.createComponent(App);
26+
const app = fixture.componentInstance;
27+
expect((app as any).title()).toMatchInlineSnapshot();
28+
});
29+
`,
30+
);
31+
32+
let content = await readFile('src/app/app.spec.ts');
33+
content = content.replace(/\r\n/g, '\n');
34+
content = content.replace(/\r/g, '\n');
35+
await writeFile('src/app/app.spec.ts', content);
36+
37+
await replaceInFile(
38+
'node_modules/@vitest/snapshot/dist/index.js',
39+
'const transformed = s.toString()',
40+
'console.log(file, JSON.stringify(snapshots, null, 2));\nconst transformed = s.toString()',
41+
);
42+
43+
await replaceInFile(
44+
'node_modules/@vitest/snapshot/dist/index.js',
45+
'const transformed = s.toString();',
46+
'const transformed = s.toString();console.log(transformed);',
2747
);
2848

2949
// First run: create snapshots
30-
const { stdout: firstRunStdout } = await ng('test');
50+
const { stdout: firstRunStdout } = await noSilentNg('test');
3151
assert.match(
3252
stripVTControlCharacters(firstRunStdout),
3353
/Snapshots\s+2 written/,
3454
'Snapshots were not written on the first run.',
3555
);
3656

57+
const snapshotContent = await readFile('src/app/__snapshots__/app.spec.ts.snap');
58+
assert.match(
59+
snapshotContent,
60+
/exports\[`App > should match file snapshot 1`\] = `"test-project"`;/,
61+
'File snapshot was not written to disk.',
62+
);
63+
3764
const specContent = await readFile('src/app/app.spec.ts');
65+
console.log(specContent);
3866
assert.match(
3967
specContent,
4068
/toMatchInlineSnapshot\(`"test-project"`\)/,
4169
'Inline snapshot was not written to the spec file.',
4270
);
4371

44-
const snapshotContent = await readFile('src/app/__snapshots__/app.spec.ts.snap');
45-
assert.match(
46-
snapshotContent,
47-
/exports\[`should match file snapshot 1`\] = `"test-project"`;/,
48-
'File snapshot was not written to disk.',
49-
);
50-
5172
// Second run: tests should pass with existing snapshots
5273
await ng('test');
5374

0 commit comments

Comments
 (0)