Skip to content

Commit c30c5cb

Browse files
committed
test: verify snapshot updates in the Vitest snapshot e2e test
Extends the Vitest snapshot e2e test to cover updating existing snapshots after a mismatch. Vitest reads the `UPDATE_SNAPSHOT` environment variable directly, which is currently the way to request an update through `ng test` without a Vitest configuration file since the builder does not forward the `--update` flag. The test now verifies that both the file snapshot and the inline snapshot are rewritten on disk with the new output and that a subsequent run passes without any further snapshot changes.
1 parent d3295e1 commit c30c5cb

1 file changed

Lines changed: 51 additions & 3 deletions

File tree

‎tests/e2e/tests/vitest/snapshot.ts‎

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ng } from '../../utils/process';
1+
import { execWithEnv, ng } from '../../utils/process';
22
import { replaceInFile, readFile, writeFile } from '../../utils/fs';
33
import { applyVitestBuilder } from '../../utils/vitest';
44
import assert from 'node:assert/strict';
@@ -60,8 +60,10 @@ describe('App', () => {
6060
// Second run: tests should pass with existing snapshots
6161
await ng('test');
6262

63-
// Modify component to break snapshots
64-
await replaceInFile('src/app/app.ts', 'test-project', 'Snapshot is broken!');
63+
// Modify component to break snapshots. The new title keeps the original value as a prefix so
64+
// the default `should render title` spec, which uses a substring assertion, continues to pass.
65+
// The snapshots compare the exact value and therefore still fail.
66+
await replaceInFile('src/app/app.ts', 'test-project', 'test-project (updated)');
6567

6668
// Third run: tests should fail with snapshot mismatch
6769
await assert.rejects(
@@ -76,4 +78,50 @@ describe('App', () => {
7678
},
7779
'Snapshot mismatch did not cause the test to fail.',
7880
);
81+
82+
// Fourth run: update the snapshots to match the new output.
83+
// Vitest reads the `UPDATE_SNAPSHOT` environment variable directly. The builder does not
84+
// currently forward Vitest's `--update` flag, so the variable is the way to request an update
85+
// without a Vitest configuration file.
86+
const { stdout: updateRunStdout } = await execWithEnv('ng', ['test'], {
87+
...process.env,
88+
UPDATE_SNAPSHOT: 'all',
89+
});
90+
assert.match(
91+
stripVTControlCharacters(updateRunStdout),
92+
/Snapshots\s+2 updated/,
93+
'Snapshots were not updated when UPDATE_SNAPSHOT=all was set.',
94+
);
95+
96+
const updatedSpecContent = await readFile('src/app/app.spec.ts');
97+
assert.match(
98+
updatedSpecContent,
99+
/toMatchInlineSnapshot\(`"test-project \(updated\)"`\)/,
100+
'Inline snapshot was not updated in the spec file.',
101+
);
102+
assert.doesNotMatch(
103+
updatedSpecContent,
104+
/toMatchInlineSnapshot\(`"test-project"`\)/,
105+
'Stale inline snapshot is still present in the spec file.',
106+
);
107+
108+
const updatedSnapshotContent = await readFile('src/app/__snapshots__/app.spec.ts.snap');
109+
assert.match(
110+
updatedSnapshotContent,
111+
/exports\[`App > should match file snapshot 1`\] = `"test-project \(updated\)"`;/,
112+
'File snapshot was not updated on disk.',
113+
);
114+
assert.doesNotMatch(
115+
updatedSnapshotContent,
116+
/"test-project"/,
117+
'Stale file snapshot is still present on disk.',
118+
);
119+
120+
// Fifth run: tests should pass with the updated snapshots and no snapshot should be rewritten
121+
const { stdout: finalRunStdout } = await ng('test');
122+
assert.doesNotMatch(
123+
stripVTControlCharacters(finalRunStdout),
124+
/Snapshots\s+\d+ (written|updated|failed|obsolete)/,
125+
'Snapshots were unexpectedly modified after being updated.',
126+
);
79127
}

0 commit comments

Comments
 (0)