diff --git a/private/react-native-fantom/runner/__tests__/snapshotUtils-test.js b/private/react-native-fantom/runner/__tests__/snapshotUtils-test.js index 62f355bc2334..36ebc9e347e9 100644 --- a/private/react-native-fantom/runner/__tests__/snapshotUtils-test.js +++ b/private/react-native-fantom/runner/__tests__/snapshotUtils-test.js @@ -8,7 +8,10 @@ * @format */ -import {updateSnapshotsAndGetJestSnapshotResult} from '../snapshotUtils'; +import { + saveInlineSnapshotsToSource, + updateSnapshotsAndGetJestSnapshotResult, +} from '../snapshotUtils'; import {SnapshotState} from 'jest-snapshot'; import fs from 'node:fs'; import os from 'node:os'; @@ -20,6 +23,61 @@ beforeEach(() => { }); afterEach(() => fs.rmSync(directory, {recursive: true, force: true})); +describe('inline snapshot source updates', () => { + it.each(['', '`"old"`'])( + 'leaves the entire source unchanged when one call has conflicting values (%s)', + argument => { + const filePath = path.join(directory, 'example-itest.js'); + const source = `expect(other).toMatchInlineSnapshot();\nexpect(value).toMatchInlineSnapshot(${argument});\n`; + fs.writeFileSync(filePath, source); + + expect(() => + saveInlineSnapshotsToSource(filePath, [ + {line: 1, snapshot: '"other"'}, + {line: 2, snapshot: '"a"'}, + {line: 2, snapshot: '"b"'}, + ]), + ).toThrow( + 'Multiple inline snapshots for the same call have different values', + ); + expect(fs.readFileSync(filePath, 'utf8')).toBe(source); + }, + ); + + it.each(['', '`"old"`'])( + 'coalesces identical values across configurations (%s)', + argument => { + const filePath = path.join(directory, 'example-itest.js'); + fs.writeFileSync( + filePath, + `expect(value).toMatchInlineSnapshot(${argument});\n`, + ); + saveInlineSnapshotsToSource(filePath, [ + {line: 1, snapshot: '"same"'}, + {line: 1, snapshot: '"same"'}, + ]); + expect(fs.readFileSync(filePath, 'utf8')).toBe( + 'expect(value).toMatchInlineSnapshot(`"same"`);\n', + ); + }, + ); + + it('writes different values to distinct calls without shifting their locations', () => { + const filePath = path.join(directory, 'example-itest.js'); + fs.writeFileSync( + filePath, + 'expect(first).toMatchInlineSnapshot();\nexpect(second).toMatchInlineSnapshot();\n', + ); + saveInlineSnapshotsToSource(filePath, [ + {line: 1, snapshot: '\nfirst\n'}, + {line: 2, snapshot: '"second"'}, + ]); + expect(fs.readFileSync(filePath, 'utf8')).toBe( + 'expect(first).toMatchInlineSnapshot(`\n first\n`);\nexpect(second).toMatchInlineSnapshot(`"second"`);\n', + ); + }); +}); + it('does not overwrite an existing snapshot on mismatch without -u', () => { const filePath = path.join(directory, 'example.snap'); fs.writeFileSync(filePath, 'exports[`test 1`] = `old hash`;\n'); diff --git a/private/react-native-fantom/runner/snapshotUtils.js b/private/react-native-fantom/runner/snapshotUtils.js index ff1edbf4629c..52374b368e45 100644 --- a/private/react-native-fantom/runner/snapshotUtils.js +++ b/private/react-native-fantom/runner/snapshotUtils.js @@ -239,9 +239,16 @@ export function saveInlineSnapshotsToSource( return; } - // Deduplicate by line (last value wins) + // Configurations can produce the same snapshot, but conflicting values + // cannot share one call. Validate before changing the source file. const byLine = new Map(); for (const snapshot of pendingSnapshots) { + const previous = byLine.get(snapshot.line); + if (previous != null && previous.snapshot !== snapshot.snapshot) { + throw new Error( + 'Fantom: Multiple inline snapshots for the same call have different values.', + ); + } byLine.set(snapshot.line, snapshot); }