Skip to content

Commit 46aabaa

Browse files
authored
fix(browser): avoid updating screenshots when toMatchScreenshot passes (#9289)
1 parent e3e659a commit 46aabaa

File tree

2 files changed

+111
-48
lines changed

2 files changed

+111
-48
lines changed

packages/browser/src/node/commands/screenshotMatcher/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ async function determineOutcome(
148148
}
149149

150150
// no reference to compare against - create one based on update settings
151-
if (reference === null || updateSnapshot === 'all') {
151+
if (reference === null) {
152152
if (updateSnapshot === 'all') {
153153
return {
154154
type: 'update-reference',
@@ -190,6 +190,16 @@ async function determineOutcome(
190190
return { type: 'matched-after-comparison' }
191191
}
192192

193+
if (updateSnapshot === 'all') {
194+
return {
195+
type: 'update-reference',
196+
reference: {
197+
image: screenshot,
198+
path: paths.reference,
199+
},
200+
}
201+
}
202+
193203
return {
194204
type: 'mismatch',
195205
reference: {

test/browser/specs/to-match-screenshot.test.ts

Lines changed: 100 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -99,53 +99,6 @@ describe('--watch', () => {
9999
},
100100
)
101101

102-
test(
103-
'with --update creates snapshots and updates them on change',
104-
async () => {
105-
const { fs, stderr, vitest } = await runInlineTests(
106-
{
107-
[testFilename]: testContent,
108-
'utils.ts': utilsContent,
109-
},
110-
{
111-
update: true,
112-
},
113-
)
114-
115-
expect(stderr).toMatchInlineSnapshot(`""`)
116-
117-
const referencePath = `__screenshots__/${testFilename}/${testName}-1-${browser}-${platform()}.png`
118-
const referenceStat = fs.statFile(referencePath)
119-
120-
fs.editFile(testFilename, content => `${content}\n`)
121-
122-
vitest.resetOutput()
123-
await vitest.waitForStdout('Test Files 1 passed')
124-
125-
expect(vitest.stdout).toContain('✓ |chromium| basic.test.ts > screenshot-snapshot')
126-
127-
const {
128-
atime,
129-
atimeMs,
130-
ctime,
131-
ctimeMs,
132-
mtime,
133-
mtimeMs,
134-
...diffs
135-
} = fs.statFile(referencePath)
136-
137-
expect(referenceStat).toEqual(expect.objectContaining(diffs))
138-
139-
expect(atime.getTime()).toBeGreaterThan(referenceStat.atime.getTime())
140-
expect(ctime.getTime()).toBeGreaterThan(referenceStat.ctime.getTime())
141-
expect(mtime.getTime()).toBeGreaterThan(referenceStat.mtime.getTime())
142-
143-
expect(atimeMs).toBeGreaterThan(referenceStat.atimeMs)
144-
expect(ctimeMs).toBeGreaterThan(referenceStat.ctimeMs)
145-
expect(mtimeMs).toBeGreaterThan(referenceStat.mtimeMs)
146-
},
147-
)
148-
149102
test(
150103
'creates a reference and fails when changing the DOM content',
151104
async () => {
@@ -168,4 +121,104 @@ describe('--watch', () => {
168121
expect(vitest.stdout).toMatch(/\d+ pixels \(ratio 0.\d{2}\) differ\./)
169122
},
170123
)
124+
125+
describe('--update', () => {
126+
test(
127+
'creates snapshot and does NOT update it if reference matches',
128+
async () => {
129+
const { fs, stderr, vitest } = await runInlineTests(
130+
{
131+
[testFilename]: testContent,
132+
'utils.ts': utilsContent,
133+
},
134+
{
135+
update: true,
136+
},
137+
)
138+
139+
expect(stderr).toMatchInlineSnapshot(`""`)
140+
141+
const osPlatform = platform()
142+
const referencePath = `__screenshots__/${testFilename}/${testName}-1-${browser}-${osPlatform}.png`
143+
const referenceStat = fs.statFile(referencePath)
144+
145+
fs.editFile(testFilename, content => `${content}\n`)
146+
147+
vitest.resetOutput()
148+
await vitest.waitForStdout('Test Files 1 passed')
149+
150+
expect(vitest.stdout).toContain('✓ |chromium| basic.test.ts > screenshot-snapshot')
151+
152+
// only atime should change since reference should NOT be updated
153+
154+
const {
155+
atime,
156+
atimeMs,
157+
...diffs
158+
} = fs.statFile(referencePath)
159+
160+
expect(referenceStat).toEqual(expect.objectContaining(diffs))
161+
162+
// win32 does not update `atime` by default
163+
if (osPlatform === 'win32') {
164+
expect(atime.getTime()).toEqual(referenceStat.atime.getTime())
165+
expect(atimeMs).toEqual(referenceStat.atimeMs)
166+
}
167+
else {
168+
expect(atime.getTime()).toBeGreaterThan(referenceStat.atime.getTime())
169+
expect(atimeMs).toBeGreaterThan(referenceStat.atimeMs)
170+
}
171+
},
172+
)
173+
174+
test(
175+
'creates snapshot and updates it if reference mismatches',
176+
async () => {
177+
const { fs, stderr, vitest } = await runInlineTests(
178+
{
179+
[testFilename]: testContent,
180+
'utils.ts': utilsContent,
181+
},
182+
{
183+
update: true,
184+
},
185+
)
186+
187+
expect(stderr).toMatchInlineSnapshot(`""`)
188+
189+
const referencePath = `__screenshots__/${testFilename}/${testName}-1-${browser}-${platform()}.png`
190+
const referenceStat = fs.statFile(referencePath)
191+
192+
fs.editFile(testFilename, content => content.replace(bgColor, '#000'))
193+
194+
vitest.resetOutput()
195+
await vitest.waitForStdout('Test Files 1 passed')
196+
197+
expect(vitest.stdout).toContain('✓ |chromium| basic.test.ts > screenshot-snapshot')
198+
199+
// atime, ctime, mtime, and size should change since reference should be updated
200+
201+
const {
202+
atime,
203+
atimeMs,
204+
ctime,
205+
ctimeMs,
206+
mtime,
207+
mtimeMs,
208+
size,
209+
...diffs
210+
} = fs.statFile(referencePath)
211+
212+
expect(referenceStat).toEqual(expect.objectContaining(diffs))
213+
214+
expect(atime.getTime()).toBeGreaterThan(referenceStat.atime.getTime())
215+
expect(ctime.getTime()).toBeGreaterThan(referenceStat.ctime.getTime())
216+
expect(mtime.getTime()).toBeGreaterThan(referenceStat.mtime.getTime())
217+
218+
expect(atimeMs).toBeGreaterThan(referenceStat.atimeMs)
219+
expect(ctimeMs).toBeGreaterThan(referenceStat.ctimeMs)
220+
expect(mtimeMs).toBeGreaterThan(referenceStat.mtimeMs)
221+
},
222+
)
223+
})
171224
})

0 commit comments

Comments
 (0)