Skip to content

Commit 01e01bf

Browse files
fix(snapshot): support mix of normal/with placeholders snapshots (#4118)
1 parent d79cb44 commit 01e01bf

File tree

4 files changed

+93
-8
lines changed

4 files changed

+93
-8
lines changed

packages/snapshot/src/port/inlineSnapshot.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,17 @@ function prepareSnapString(snap: string, source: string, index: number) {
9696

9797
const startRegex = /(?:toMatchInlineSnapshot|toThrowErrorMatchingInlineSnapshot)\s*\(\s*(?:\/\*[\S\s]*\*\/\s*|\/\/.*\s+)*\s*[\w_$]*(['"`\)])/m
9898
export function replaceInlineSnap(code: string, s: MagicString, index: number, newSnap: string) {
99-
const startMatch = startRegex.exec(code.slice(index))
100-
if (!startMatch)
99+
const codeStartingAtIndex = code.slice(index)
100+
101+
const startMatch = startRegex.exec(codeStartingAtIndex)
102+
103+
const firstKeywordMatch = /toMatchInlineSnapshot|toThrowErrorMatchingInlineSnapshot/.exec(codeStartingAtIndex)
104+
105+
if (!startMatch || startMatch.index !== firstKeywordMatch?.index)
101106
return replaceObjectSnap(code, s, index, newSnap)
102107

103108
const quote = startMatch[1]
104-
const startIndex = index + startMatch.index! + startMatch[0].length
109+
const startIndex = index + startMatch.index + startMatch[0].length
105110
const snapString = prepareSnapString(newSnap, code, index)
106111

107112
if (quote === ')') {

test/snapshots/test-update/snapshots-inline-js.test.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,35 @@ describe('snapshots with properties', () => {
3434
})
3535

3636
test('with snapshot', () => {
37-
expect({ foo: 'bar' }).toMatchInlineSnapshot({ foo: expect.any(String) }, `
37+
expect({ first: { second: { foo: 'bar' } } }).toMatchInlineSnapshot({ first: { second: { foo: expect.any(String) } } }, `
3838
Object {
39-
"foo": Any<String>,
39+
"first": Object {
40+
"second": Object {
41+
"foo": Any<String>,
42+
},
43+
},
44+
}
45+
`)
46+
})
47+
48+
test('mixed with and without snapshot', () => {
49+
expect({ first: { second: { foo: 'bar' } } }).toMatchInlineSnapshot({ first: { second: { foo: expect.any(String) } } }, `
50+
Object {
51+
"first": Object {
52+
"second": Object {
53+
"foo": Any<String>,
54+
},
55+
},
56+
}
57+
`)
58+
59+
expect({ first: { second: { foo: 'zed' } } }).toMatchInlineSnapshot(`
60+
Object {
61+
"first": Object {
62+
"second": Object {
63+
"foo": "zed",
64+
},
65+
},
4066
}
4167
`)
4268
})

test/snapshots/test/__snapshots__/shapshots.test.ts.snap

+28-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,35 @@ describe('snapshots with properties', () => {
3737
})
3838
3939
test('with snapshot', () => {
40-
expect({ foo: 'bar' }).toMatchInlineSnapshot({ foo: expect.any(String) }, \`
40+
expect({ first: { second: { foo: 'bar' } } }).toMatchInlineSnapshot({ first: { second: { foo: expect.any(String) } } }, \`
4141
Object {
42-
\\"foo\\": Any<String>,
42+
\\"first\\": Object {
43+
\\"second\\": Object {
44+
\\"foo\\": Any<String>,
45+
},
46+
},
47+
}
48+
\`)
49+
})
50+
51+
test('mixed with and without snapshot', () => {
52+
expect({ first: { second: { foo: 'bar' } } }).toMatchInlineSnapshot({ first: { second: { foo: expect.any(String) } } }, \`
53+
Object {
54+
\\"first\\": Object {
55+
\\"second\\": Object {
56+
\\"foo\\": Any<String>,
57+
},
58+
},
59+
}
60+
\`)
61+
62+
expect({ first: { second: { foo: 'zed' } } }).toMatchInlineSnapshot(\`
63+
Object {
64+
\\"first\\": Object {
65+
\\"second\\": Object {
66+
\\"foo\\": \\"zed\\",
67+
},
68+
},
4369
}
4470
\`)
4571
})

test/snapshots/tools/inline-test-template.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,34 @@ describe('snapshots with properties', () => {
1818
})
1919

2020
test('with snapshot', () => {
21-
expect({ foo: 'bar' }).toMatchInlineSnapshot({ foo: expect.any(String) }, '')
21+
expect({ first: { second: { foo: 'bar' } } }).toMatchInlineSnapshot({ first: { second: { foo: expect.any(String) } } }, `
22+
Object {
23+
"first": Object {
24+
"wrong": Any<String>,
25+
"second": null,
26+
}
27+
}
28+
`)
29+
})
30+
31+
test('mixed with and without snapshot', () => {
32+
expect({ first: { second: { foo: 'bar' } } }).toMatchInlineSnapshot({ first: { second: { foo: expect.any(String) } } }, `
33+
Object {
34+
"first": Object {
35+
"wrong": Any<String>,
36+
"second": null,
37+
}
38+
}
39+
`)
40+
41+
expect({ first: { second: { foo: 'zed' } } }).toMatchInlineSnapshot(`
42+
Object {
43+
"first": Object {
44+
"second": {
45+
"foo": "zed"
46+
}
47+
}
48+
}
49+
`)
2250
})
2351
})

0 commit comments

Comments
 (0)