Skip to content

Commit 702aa7d

Browse files
committed
Mark snapshots as obsolete when moved to an inline snapshot
Fixes #6655
1 parent 0f525c5 commit 702aa7d

3 files changed

Lines changed: 76 additions & 1 deletion

File tree

e2e/__tests__/__snapshots__/to_match_inline_snapshot.test.js.snap

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@ Object {
9696
"
9797
`;
9898
99+
exports[`removes obsolete external snapshots: external snapshot cleaned 1`] = `
100+
"test('removes obsolete external snapshots', () => {
101+
expect('1').toMatchInlineSnapshot(\`\\"1\\"\`);
102+
});
103+
"
104+
`;
105+
106+
exports[`removes obsolete external snapshots: initial write 1`] = `
107+
"
108+
test('removes obsolete external snapshots', () => {
109+
expect('1').toMatchSnapshot();
110+
});
111+
"
112+
`;
113+
114+
exports[`removes obsolete external snapshots: inline snapshot written 1`] = `
115+
"test('removes obsolete external snapshots', () => {
116+
expect('1').toMatchInlineSnapshot(\`\\"1\\"\`);
117+
});
118+
"
119+
`;
120+
99121
exports[`supports async matchers 1`] = `
100122
"test('inline snapshots', async () => {
101123
expect(Promise.resolve('success')).resolves.toMatchInlineSnapshot(

e2e/__tests__/to_match_inline_snapshot.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,54 @@ test('handles property matchers', () => {
130130
}
131131
});
132132

133+
test('removes obsolete external snapshots', () => {
134+
const filename = 'removes-obsolete-external-snapshots.test.js';
135+
const snapshotPath = path.join(
136+
TESTS_DIR,
137+
'__snapshots__',
138+
filename + '.snap',
139+
);
140+
const template = makeTemplate(`
141+
test('removes obsolete external snapshots', () => {
142+
expect('1').$1();
143+
});
144+
`);
145+
146+
{
147+
writeFiles(TESTS_DIR, {[filename]: template(['toMatchSnapshot'])});
148+
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
149+
const fileAfter = readFile(filename);
150+
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
151+
expect(status).toBe(0);
152+
expect(fileAfter).toMatchSnapshot('initial write');
153+
expect(fs.existsSync(snapshotPath)).toEqual(true);
154+
}
155+
156+
{
157+
writeFiles(TESTS_DIR, {[filename]: template(['toMatchInlineSnapshot'])});
158+
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
159+
const fileAfter = readFile(filename);
160+
expect(stderr).toMatch('Snapshots: 1 obsolete, 1 written, 1 total');
161+
expect(status).toBe(1);
162+
expect(fileAfter).toMatchSnapshot('inline snapshot written');
163+
expect(fs.existsSync(snapshotPath)).toEqual(true);
164+
}
165+
166+
{
167+
const {stderr, status} = runJest(DIR, [
168+
'-w=1',
169+
'--ci=false',
170+
filename,
171+
'-u',
172+
]);
173+
const fileAfter = readFile(filename);
174+
expect(stderr).toMatch('Snapshots: 1 file removed, 1 passed, 1 total');
175+
expect(status).toBe(0);
176+
expect(fileAfter).toMatchSnapshot('external snapshot cleaned');
177+
expect(fs.existsSync(snapshotPath)).toEqual(false);
178+
}
179+
});
180+
133181
test('supports async matchers', () => {
134182
const filename = 'async-matchers.test.js';
135183
const test = `

packages/jest-snapshot/src/State.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,12 @@ export default class SnapshotState {
170170
key = testNameToKey(testName, count);
171171
}
172172

173-
this._uncheckedKeys.delete(key);
173+
// Do not mark the snapshot as "checked" if the snapshot is inline and
174+
// there's an external snapshot. This way the external snapshot can be
175+
// removed with `--updateSnapshot`.
176+
if (!(isInline && this._snapshotData[key])) {
177+
this._uncheckedKeys.delete(key);
178+
}
174179

175180
const receivedSerialized = serialize(received);
176181
const expected = isInline ? inlineSnapshot : this._snapshotData[key];

0 commit comments

Comments
 (0)