Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion tests/e2e/tests/vitest/snapshot.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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.',
);
}
Loading