Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/browser/src/client/tester/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function createBrowserRunner(
this.config.attachmentsDir,
'failure-screenshots',
basename(task.file.filepath),
`${getFailureScreenshotName(task, retry, repeats)}.png`,
`${getFailureScreenshotName(task, this.config.name, retry, repeats)}.png`,
),
})
.catch((err) => {
Expand Down Expand Up @@ -462,8 +462,14 @@ async function updateTestFilesLocations(files: File[], sourceMaps: Map<string, a
await Promise.all(promises)
}

function getFailureScreenshotName(test: Test, retry: number, repeats: number) {
const name = test.fullTestName.replace(/\W/g, '-')
// the browser instances run the same test at the same time, so each one needs its own file
function getFailureScreenshotName(
test: Test,
projectName: string | undefined,
retry: number,
repeats: number,
) {
const name = `${projectName ? `${projectName}-` : ''}${test.fullTestName}`.replace(/\W/g, '-')
const maxRetries = typeof test.retry === 'number' ? test.retry : (test.retry?.count ?? 0)
// add '<name>-<repeat>-<retry>' suffix only when test enables retry or repeat
if (!maxRetries && !test.repeats) {
Expand Down
55 changes: 45 additions & 10 deletions test/browser/specs/failure-screenshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { resolve } from 'pathe'
import { describe, expect, test } from 'vitest'
import { runInlineTests } from '../../test-utils'
import utilsContent from '../fixtures/expect-dom/utils?raw'
import { instances, runInlineBrowserTests } from './utils'

const testFilename = 'basic.test.ts'

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

expect(screenshotPath).toContain(
'.vitest/attachments/failure-screenshots/basic.test.ts/screenshot-unstable.png',
'.vitest/attachments/failure-screenshots/basic.test.ts/chromium-screenshot-unstable.png',
)
})

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

expect(screenshotPath).toContain(
'.vitest/attachments/failure-screenshots/basic.test.ts/screenshot-soft-then-fail.png',
'.vitest/attachments/failure-screenshots/basic.test.ts/chromium-screenshot-soft-then-fail.png',
)
})
})
Expand Down Expand Up @@ -142,19 +143,19 @@ describe('failure screenshots', () => {
" FAIL |chromium| basic.test.ts > repeated (initial run)
AssertionError: expected 1 to be 10 // Object.is equality
Failure screenshot:
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/repeated-0-0.png
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-repeated-0-0.png
FAIL |chromium| basic.test.ts > repeated (1st retry)
AssertionError: expected 2 to be 10 // Object.is equality
Failure screenshot:
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/repeated-0-1.png
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-repeated-0-1.png
FAIL |chromium| basic.test.ts > repeated (1st repeat)
AssertionError: expected 3 to be 10 // Object.is equality
Failure screenshot:
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/repeated-1-0.png
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-repeated-1-0.png
FAIL |chromium| basic.test.ts > repeated (1st retry of 1st repeat)
AssertionError: expected 4 to be 10 // Object.is equality
Failure screenshot:
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/repeated-1-1.png"
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-repeated-1-1.png"
`)
expect(new Set(readReportedScreenshots(stderr)).size).toBe(4)
})
Expand All @@ -177,14 +178,48 @@ describe('failure screenshots', () => {
" FAIL |chromium| basic.test.ts > soft (initial run, 1st retry)
AssertionError: expected 1 to be 2 // Object.is equality
Failure screenshots:
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/soft-0-0.png
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/soft-0-1.png
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-soft-0-0.png
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-soft-0-1.png
FAIL |chromium| basic.test.ts > soft (initial run, 1st retry)
AssertionError: expected 2 to be 3 // Object.is equality
Failure screenshots:
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/soft-0-0.png
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/soft-0-1.png"
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-soft-0-0.png
- <root>/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-soft-0-1.png"
`)
})
})

test('each browser instance saves its own screenshot', async () => {
const { root, results } = await runInlineBrowserTests({
[testFilename]: /* ts */ `
import { expect, test } from 'vitest'
test('adds an item', () => {
document.body.innerHTML = '<button>Add</button>'
expect(1).toBe(2)
})
`,
})

const screenshots = Object.fromEntries(
results.map((module) => {
const [testCase] = module.children.allTests()
const screenshot = testCase
.artifacts()
.find((artifact) => artifact.type === 'internal:failureScreenshot')
return [module.project.name, screenshot?.attachments[0]?.originalPath]
}),
)
expect(screenshots).toEqual(
Object.fromEntries(
instances.map(({ browser }) => [
browser,
resolve(
root,
`.vitest/attachments/failure-screenshots/basic.test.ts/${browser}-adds-an-item.png`,
),
]),
),
)
})
})
2 changes: 1 addition & 1 deletion test/e2e/snapshots/domain-aria-inline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ test('aria inline snapshot', async () => {
Error: Snapshot \`semantic match with regex in snapshot 1\` mismatched
Failure screenshot:
- snapshots/fixtures/domain-aria-inline/.vitest/attachments/failure-screenshots/basic.test.ts/semantic-match-with-regex-in-snapshot.png
- snapshots/fixtures/domain-aria-inline/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-semantic-match-with-regex-in-snapshot.png
- Expected
+ Received
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/snapshots/domain-aria.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ test('aria snapshot', { tags: ['browser'] }, async () => {
Error: Snapshot \`semantic match with regex in snapshot 1\` mismatched
Failure screenshot:
- snapshots/fixtures/domain-aria/.vitest/attachments/failure-screenshots/basic.test.ts/semantic-match-with-regex-in-snapshot.png
- snapshots/fixtures/domain-aria/.vitest/attachments/failure-screenshots/basic.test.ts/chromium-semantic-match-with-regex-in-snapshot.png
- Expected
+ Received
Expand Down
Loading