[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(snapshot): support mix of normal/with placeholders snapshots (#4118)

authored by

RobertPechaCZ and committed by
GitHub
(Sep 14, 2023, 10:19 AM +0200) 01e01bfc d79cb44b

+93 -8
+28 -2
test/snapshots/test-update/snapshots-inline-js.test.js
··· 34 34 }) 35 35 36 36 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) } } }, ` 38 38 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 + }, 40 66 } 41 67 `) 42 68 })
+29 -1
test/snapshots/tools/inline-test-template.js
··· 18 18 }) 19 19 20 20 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 + `) 22 50 }) 23 51 })
+8 -3
packages/snapshot/src/port/inlineSnapshot.ts
··· 96 96 97 97 const startRegex = /(?:toMatchInlineSnapshot|toThrowErrorMatchingInlineSnapshot)\s*\(\s*(?:\/\*[\S\s]*\*\/\s*|\/\/.*\s+)*\s*[\w_$]*(['"`\)])/m 98 98 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) 101 106 return replaceObjectSnap(code, s, index, newSnap) 102 107 103 108 const quote = startMatch[1] 104 - const startIndex = index + startMatch.index! + startMatch[0].length 109 + const startIndex = index + startMatch.index + startMatch[0].length 105 110 const snapString = prepareSnapString(newSnap, code, index) 106 111 107 112 if (quote === ')') {
+28 -2
test/snapshots/test/__snapshots__/shapshots.test.ts.snap
··· 37 37 }) 38 38 39 39 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) } } }, \` 41 41 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 + }, 43 69 } 44 70 \`) 45 71 })