Skip to content

Commit e0e8e00

Browse files
authored
fix(browser): save failure screenshots per instance (#12042)
1 parent 2123874 commit e0e8e00

4 files changed

Lines changed: 56 additions & 15 deletions

File tree

‎packages/browser/src/client/tester/runner.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ function createBrowserRunner(
203203
this.config.attachmentsDir,
204204
'failure-screenshots',
205205
basename(task.file.filepath),
206-
`${getFailureScreenshotName(task, retry, repeats)}.png`,
206+
`${getFailureScreenshotName(task, this.config.name, retry, repeats)}.png`,
207207
),
208208
})
209209
.catch((err) => {
@@ -462,8 +462,14 @@ async function updateTestFilesLocations(files: File[], sourceMaps: Map<string, a
462462
await Promise.all(promises)
463463
}
464464

465-
function getFailureScreenshotName(test: Test, retry: number, repeats: number) {
466-
const name = test.fullTestName.replace(/\W/g, '-')
465+
// the browser instances run the same test at the same time, so each one needs its own file
466+
function getFailureScreenshotName(
467+
test: Test,
468+
projectName: string | undefined,
469+
retry: number,
470+
repeats: number,
471+
) {
472+
const name = `${projectName ? `${projectName}-` : ''}${test.fullTestName}`.replace(/\W/g, '-')
467473
const maxRetries = typeof test.retry === 'number' ? test.retry : (test.retry?.count ?? 0)
468474
// add '<name>-<repeat>-<retry>' suffix only when test enables retry or repeat
469475
if (!maxRetries && !test.repeats) {

‎test/browser/specs/failure-screenshot.test.ts‎

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { resolve } from 'pathe'
44
import { describe, expect, test } from 'vitest'
55
import { runInlineTests } from '../../test-utils'
66
import utilsContent from '../fixtures/expect-dom/utils?raw'
7+
import { instances, runInlineBrowserTests } from './utils'
78

89
const testFilename = 'basic.test.ts'
910

@@ -76,7 +77,7 @@ describe('failure screenshots', () => {
7677
const screenshotPath = extractScreenshotPath(stderr)
7778

7879
expect(screenshotPath).toContain(
79-
'.vitest/attachments/failure-screenshots/basic.test.ts/screenshot-unstable.png',
80+
'.vitest/attachments/failure-screenshots/basic.test.ts/chromium-screenshot-unstable.png',
8081
)
8182
})
8283

@@ -103,7 +104,7 @@ describe('failure screenshots', () => {
103104
const screenshotPath = extractScreenshotPath(stderr)
104105

105106
expect(screenshotPath).toContain(
106-
'.vitest/attachments/failure-screenshots/basic.test.ts/screenshot-soft-then-fail.png',
107+
'.vitest/attachments/failure-screenshots/basic.test.ts/chromium-screenshot-soft-then-fail.png',
107108
)
108109
})
109110
})
@@ -142,19 +143,19 @@ describe('failure screenshots', () => {
142143
" FAIL |chromium| basic.test.ts > repeated (initial run)
143144
AssertionError: expected 1 to be 10 // Object.is equality
144145
Failure screenshot:
145-
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/repeated-0-0.png
146+
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-repeated-0-0.png
146147
FAIL |chromium| basic.test.ts > repeated (1st retry)
147148
AssertionError: expected 2 to be 10 // Object.is equality
148149
Failure screenshot:
149-
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/repeated-0-1.png
150+
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-repeated-0-1.png
150151
FAIL |chromium| basic.test.ts > repeated (1st repeat)
151152
AssertionError: expected 3 to be 10 // Object.is equality
152153
Failure screenshot:
153-
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/repeated-1-0.png
154+
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-repeated-1-0.png
154155
FAIL |chromium| basic.test.ts > repeated (1st retry of 1st repeat)
155156
AssertionError: expected 4 to be 10 // Object.is equality
156157
Failure screenshot:
157-
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/repeated-1-1.png"
158+
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-repeated-1-1.png"
158159
`)
159160
expect(new Set(readReportedScreenshots(stderr)).size).toBe(4)
160161
})
@@ -177,14 +178,48 @@ describe('failure screenshots', () => {
177178
" FAIL |chromium| basic.test.ts > soft (initial run, 1st retry)
178179
AssertionError: expected 1 to be 2 // Object.is equality
179180
Failure screenshots:
180-
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/soft-0-0.png
181-
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/soft-0-1.png
181+
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-soft-0-0.png
182+
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-soft-0-1.png
182183
FAIL |chromium| basic.test.ts > soft (initial run, 1st retry)
183184
AssertionError: expected 2 to be 3 // Object.is equality
184185
Failure screenshots:
185-
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/soft-0-0.png
186-
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/soft-0-1.png"
186+
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-soft-0-0.png
187+
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-soft-0-1.png"
187188
`)
188189
})
189190
})
191+
192+
test('each browser instance saves its own screenshot', async () => {
193+
const { root, results } = await runInlineBrowserTests({
194+
[testFilename]: /* ts */ `
195+
import { expect, test } from 'vitest'
196+
197+
test('adds an item', () => {
198+
document.body.innerHTML = '<button>Add</button>'
199+
expect(1).toBe(2)
200+
})
201+
`,
202+
})
203+
204+
const screenshots = Object.fromEntries(
205+
results.map((module) => {
206+
const [testCase] = module.children.allTests()
207+
const screenshot = testCase
208+
.artifacts()
209+
.find((artifact) => artifact.type === 'internal:failureScreenshot')
210+
return [module.project.name, screenshot?.attachments[0]?.originalPath]
211+
}),
212+
)
213+
expect(screenshots).toEqual(
214+
Object.fromEntries(
215+
instances.map(({ browser }) => [
216+
browser,
217+
resolve(
218+
root,
219+
`.vitest/attachments/failure-screenshots/basic.test.ts/${browser}-adds-an-item.png`,
220+
),
221+
]),
222+
),
223+
)
224+
})
190225
})

‎test/e2e/snapshots/domain-aria-inline.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ test('aria inline snapshot', async () => {
9696
Error: Snapshot \`semantic match with regex in snapshot 1\` mismatched
9797
9898
Failure screenshot:
99-
- snapshots/fixtures/domain-aria-inline/.vitest/attachments/failure-screenshots/basic.test.ts/semantic-match-with-regex-in-snapshot.png
99+
- snapshots/fixtures/domain-aria-inline/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-semantic-match-with-regex-in-snapshot.png
100100
101101
- Expected
102102
+ Received

‎test/e2e/snapshots/domain-aria.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ test('aria snapshot', { tags: ['browser'] }, async () => {
111111
Error: Snapshot \`semantic match with regex in snapshot 1\` mismatched
112112
113113
Failure screenshot:
114-
- snapshots/fixtures/domain-aria/.vitest/attachments/failure-screenshots/basic.test.ts/semantic-match-with-regex-in-snapshot.png
114+
- snapshots/fixtures/domain-aria/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-semantic-match-with-regex-in-snapshot.png
115115
116116
- Expected
117117
+ Received

0 commit comments

Comments
 (0)