Skip to content

Commit b13546b

Browse files
authored
Keep Fantom CI snapshot baselines read-only (#59895)
Summary: Run Fantom with Jest's CI mode on every attempt so failed snapshot comparisons cannot rewrite their expected baselines. Retain retries for transient failures, but compare against the same committed snapshots each time. Also enforce the existing snapshot writer's explicit-update guard at this point in the stack, so neither CI mode nor the default update mode can overwrite an existing mismatch. Use real Jest SnapshotState instances to verify existing/missing baselines and explicit updates. Changelog: [Internal] Prevent snapshot updates during Fantom CI runs. Reviewed By: zeyap Differential Revision: D130682365 Pull Request resolved: #59895
1 parent e87af1a commit b13546b

3 files changed

Lines changed: 95 additions & 1 deletion

File tree

‎.github/actions/run-fantom-tests/action.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ runs:
2727
run: |
2828
for attempt in 1 2 3; do
2929
echo "Attempt $attempt of 3"
30-
if yarn fantom; then
30+
if yarn fantom --ci; then
3131
exit 0
3232
fi
3333
if [ "$attempt" -lt 3 ]; then
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
import type {TestSnapshotResults} from '../../runtime/snapshotContext';
12+
13+
import {updateSnapshotsAndGetJestSnapshotResult} from '../snapshotUtils';
14+
import {SnapshotState} from 'jest-snapshot';
15+
import fs from 'node:fs';
16+
import os from 'node:os';
17+
import path from 'node:path';
18+
19+
function evaluate(state: SnapshotState, snapshotResults: TestSnapshotResults) {
20+
return updateSnapshotsAndGetJestSnapshotResult(state, [snapshotResults]);
21+
}
22+
23+
describe('Fantom snapshot update modes', () => {
24+
let directory: string;
25+
26+
beforeEach(() => {
27+
directory = fs.mkdtempSync(path.join(os.tmpdir(), 'fantom-snapshot-ci-'));
28+
});
29+
30+
afterEach(() => {
31+
fs.rmSync(directory, {recursive: true, force: true});
32+
});
33+
34+
it('preserves existing baselines in CI and default update modes', () => {
35+
const file = path.join(directory, 'example.snap');
36+
const contents =
37+
'// Jest Snapshot v1, https://goo.gl/fbAQLP\n\nexports[`test 1`] = `committed baseline`;\n';
38+
for (const mode of ['none', 'new'] as const) {
39+
fs.writeFileSync(file, contents);
40+
const state = new SnapshotState(file, {
41+
updateSnapshot: mode,
42+
rootDir: directory,
43+
snapshotFormat: {},
44+
});
45+
const result = evaluate(state, {
46+
'test 1': {pass: false, value: 'received value'},
47+
});
48+
49+
expect(fs.readFileSync(file, 'utf8')).toBe(contents);
50+
expect(result.unmatched).toBe(1);
51+
expect(result.updated).toBe(0);
52+
}
53+
});
54+
55+
it('does not create missing baselines in CI mode', () => {
56+
const file = path.join(directory, 'missing.snap');
57+
const state = new SnapshotState(file, {
58+
updateSnapshot: 'none',
59+
rootDir: directory,
60+
snapshotFormat: {},
61+
});
62+
const result = evaluate(state, {
63+
'test 1': {pass: false, value: 'received value'},
64+
});
65+
66+
expect(result.unmatched).toBe(1);
67+
expect(result.added).toBe(0);
68+
expect(fs.existsSync(file)).toBe(false);
69+
});
70+
71+
it('allows explicit baseline updates outside CI', () => {
72+
const file = path.join(directory, 'example.snap');
73+
fs.writeFileSync(
74+
file,
75+
'// Jest Snapshot v1, https://goo.gl/fbAQLP\n\nexports[`test 1`] = `committed baseline`;\n',
76+
);
77+
const state = new SnapshotState(file, {
78+
updateSnapshot: 'all',
79+
rootDir: directory,
80+
snapshotFormat: {},
81+
});
82+
const result = evaluate(state, {
83+
'test 1': {pass: false, value: 'received value'},
84+
});
85+
86+
expect(result.updated).toBe(1);
87+
expect(fs.readFileSync(file, 'utf8')).toContain('received value');
88+
});
89+
});

‎private/react-native-fantom/runner/snapshotUtils.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ export const updateSnapshotsAndGetJestSnapshotResult = (
320320
continue;
321321
}
322322

323+
if (snapshotState._updateSnapshot !== 'all') {
324+
snapshotState.unmatched++;
325+
snapshotState._uncheckedKeys.delete(key);
326+
continue;
327+
}
323328
snapshotState._dirty = true;
324329
snapshotState._snapshotData[key] = addExtraLineBreaks(result.value);
325330
snapshotState.updated++;

0 commit comments

Comments
 (0)