diff --git a/tests/e2e/tests/vitest/snapshot.ts b/tests/e2e/tests/vitest/snapshot.ts index f099ba6f8d30..6e42b9585cac 100644 --- a/tests/e2e/tests/vitest/snapshot.ts +++ b/tests/e2e/tests/vitest/snapshot.ts @@ -1,4 +1,4 @@ -import { ng } from '../../utils/process'; +import { execWithEnv, ng } from '../../utils/process'; import { replaceInFile, readFile, writeFile } from '../../utils/fs'; import { applyVitestBuilder } from '../../utils/vitest'; import assert from 'node:assert/strict'; @@ -76,4 +76,50 @@ describe('App', () => { }, 'Snapshot mismatch did not cause the test to fail.', ); + + // Fourth run: update the snapshots to match the new output. + // Vitest reads the `UPDATE_SNAPSHOT` environment variable directly. The builder does not + // currently forward Vitest's `--update` flag, so the variable is the way to request an update + // without a Vitest configuration file. + const { stdout: updateRunStdout } = await execWithEnv('ng', ['test'], { + ...process.env, + UPDATE_SNAPSHOT: 'all', + }); + assert.match( + stripVTControlCharacters(updateRunStdout), + /Snapshots\s+2 updated/, + 'Snapshots were not updated when UPDATE_SNAPSHOT=all was set.', + ); + + const updatedSpecContent = await readFile('src/app/app.spec.ts'); + assert.match( + updatedSpecContent, + /toMatchInlineSnapshot\(`"Snapshot is broken!"`\)/, + 'Inline snapshot was not updated in the spec file.', + ); + assert.doesNotMatch( + updatedSpecContent, + /toMatchInlineSnapshot\(`"test-project"`\)/, + 'Stale inline snapshot is still present in the spec file.', + ); + + const updatedSnapshotContent = await readFile('src/app/__snapshots__/app.spec.ts.snap'); + assert.match( + updatedSnapshotContent, + /exports\[`App > should match file snapshot 1`\] = `"Snapshot is broken!"`;/, + 'File snapshot was not updated on disk.', + ); + assert.doesNotMatch( + updatedSnapshotContent, + /"test-project"/, + 'Stale file snapshot is still present on disk.', + ); + + // Fifth run: tests should pass with the updated snapshots and no snapshot should be rewritten + const { stdout: finalRunStdout } = await ng('test'); + assert.doesNotMatch( + stripVTControlCharacters(finalRunStdout), + /Snapshots\s+\d+ (written|updated|failed|obsolete)/, + 'Snapshots were unexpectedly modified after being updated.', + ); }