From 2280a0ad61de0b44688e4b8ea6fd246c609b8e47 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Thu, 13 Nov 2025 13:27:56 -0500 Subject: [PATCH] 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. --- tests/legacy-cli/e2e/tests/vitest/snapshot.ts | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/tests/legacy-cli/e2e/tests/vitest/snapshot.ts b/tests/legacy-cli/e2e/tests/vitest/snapshot.ts index d610f8f861dc..f099ba6f8d30 100644 --- a/tests/legacy-cli/e2e/tests/vitest/snapshot.ts +++ b/tests/legacy-cli/e2e/tests/vitest/snapshot.ts @@ -1,5 +1,5 @@ import { ng } from '../../utils/process'; -import { appendToFile, replaceInFile, readFile } from '../../utils/fs'; +import { replaceInFile, readFile, writeFile } from '../../utils/fs'; import { applyVitestBuilder } from '../../utils/vitest'; import assert from 'node:assert/strict'; import { stripVTControlCharacters } from 'node:util'; @@ -9,23 +9,32 @@ export default async function () { await applyVitestBuilder(); // Add snapshot assertions to the test file - await appendToFile( + await replaceInFile( 'src/app/app.spec.ts', + `describe('App', () => {`, ` - it('should match file snapshot', () => { - const fixture = TestBed.createComponent(App); - const app = fixture.componentInstance; - expect((app as any).title()).toMatchSnapshot(); - }); +describe('App', () => { + it('should match file snapshot', () => { + const fixture = TestBed.createComponent(App); + const app = fixture.componentInstance; + expect((app as any).title()).toMatchSnapshot(); + }); - it('should match inline snapshot', () => { - const fixture = TestBed.createComponent(App); - const app = fixture.componentInstance; - expect((app as any).title()).toMatchInlineSnapshot(); - }); - `, + it('should match inline snapshot', () => { + const fixture = TestBed.createComponent(App); + const app = fixture.componentInstance; + expect((app as any).title()).toMatchInlineSnapshot(); + }); +`, ); + // Synchronize line endings for Vitest which currently may miscalculate line counts + // with mixed file line endings. + let content = await readFile('src/app/app.spec.ts'); + content = content.replace(/\r\n/g, '\n'); + content = content.replace(/\r/g, '\n'); + await writeFile('src/app/app.spec.ts', content); + // First run: create snapshots const { stdout: firstRunStdout } = await ng('test'); assert.match( @@ -44,7 +53,7 @@ export default async function () { const snapshotContent = await readFile('src/app/__snapshots__/app.spec.ts.snap'); assert.match( snapshotContent, - /exports\[`should match file snapshot 1`\] = `"test-project"`;/, + /exports\[`App > should match file snapshot 1`\] = `"test-project"`;/, 'File snapshot was not written to disk.', );