Skip to content

Commit 8a53e2d

Browse files
clydinhybrist
authored andcommitted
test: stabilize Vitest snapshot E2E test on Windows
This commit introduces several changes to fix a non-deterministic E2E test for Vitest snapshots that was failing on Windows. The primary fix is to standardize the line endings of the test spec file to LF (`\n`) before running the test. This works around an issue where Vitest can miscalculate line counts when encountering mixed line endings (CRLF and LF), which is common in Windows environments. Additionally, the snapshot content assertion has been updated to match Vitest's current format, which includes the test suite name. The file modification logic was also refactored to use `replaceInFile` for better reliability. (cherry picked from commit 8bde4a3)
1 parent 6576bb5 commit 8a53e2d

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ng } from '../../utils/process';
2-
import { appendToFile, replaceInFile, readFile } from '../../utils/fs';
2+
import { 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';
@@ -9,23 +9,32 @@ export default async function () {
99
await applyVitestBuilder();
1010

1111
// Add snapshot assertions to the test file
12-
await appendToFile(
12+
await replaceInFile(
1313
'src/app/app.spec.ts',
14+
`describe('App', () => {`,
1415
`
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-
});
16+
describe('App', () => {
17+
it('should match file snapshot', () => {
18+
const fixture = TestBed.createComponent(App);
19+
const app = fixture.componentInstance;
20+
expect((app as any).title()).toMatchSnapshot();
21+
});
2022
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-
`,
23+
it('should match inline snapshot', () => {
24+
const fixture = TestBed.createComponent(App);
25+
const app = fixture.componentInstance;
26+
expect((app as any).title()).toMatchInlineSnapshot();
27+
});
28+
`,
2729
);
2830

31+
// Synchronize line endings for Vitest which currently may miscalculate line counts
32+
// with mixed file line endings.
33+
let content = await readFile('src/app/app.spec.ts');
34+
content = content.replace(/\r\n/g, '\n');
35+
content = content.replace(/\r/g, '\n');
36+
await writeFile('src/app/app.spec.ts', content);
37+
2938
// First run: create snapshots
3039
const { stdout: firstRunStdout } = await ng('test');
3140
assert.match(
@@ -44,7 +53,7 @@ export default async function () {
4453
const snapshotContent = await readFile('src/app/__snapshots__/app.spec.ts.snap');
4554
assert.match(
4655
snapshotContent,
47-
/exports\[`should match file snapshot 1`\] = `"test-project"`;/,
56+
/exports\[`App > should match file snapshot 1`\] = `"test-project"`;/,
4857
'File snapshot was not written to disk.',
4958
);
5059

0 commit comments

Comments
 (0)