Skip to content
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');
Expand Down
9 changes: 8 additions & 1 deletion private/react-native-fantom/runner/snapshotUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, PendingInlineSnapshot>();
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);
}

Expand Down