Skip to content

Commit 2254356

Browse files
macariesheremet-va
authored andcommitted
fix(ui): process artifact attachments when generating HTML reporter (#9472)
1 parent e057281 commit 2254356

File tree

7 files changed

+97
-5
lines changed

7 files changed

+97
-5
lines changed

packages/ui/node/reporter.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ export default class HTMLReporter implements Reporter {
9191
promises.push(this.processAttachment(attachment))
9292
}
9393
})
94+
task.artifacts.forEach((artifact) => {
95+
const attachments = artifact.attachments
96+
if (attachments) {
97+
attachments.forEach((attachment) => {
98+
promises.push(this.processAttachment(attachment))
99+
})
100+
}
101+
})
94102
}
95103
else {
96104
task.tasks.forEach(processAttachments)

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { test } from 'vitest'
2+
import { server } from 'vitest/browser'
3+
4+
test('visual regression test', async ({ expect, onTestFinished }) => {
5+
const screenshotName = 'visual-regression-screenshot.png'
6+
7+
onTestFinished(async () => {
8+
if (server.config.snapshotOptions.updateSnapshot !== 'none') {
9+
await server.commands.removeFile(`fixtures-browser/${screenshotName}`)
10+
}
11+
})
12+
13+
await expect(expect(document.body).toMatchScreenshot(screenshotName)).rejects.toThrowError(
14+
'No existing reference screenshot found',
15+
)
16+
})

test/ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"devDependencies": {
1212
"@testing-library/dom": "^10.4.1",
13+
"@vitest/browser-playwright": "workspace:*",
1314
"happy-dom": "latest",
1415
"vitest": "workspace:*"
1516
}

test/ui/test/html-report.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ test.describe('html report', () => {
6666
await page.goto(pageUrl)
6767

6868
// dashboard
69-
await expect(page.locator('[aria-labelledby=tests]')).toContainText('13 Pass 1 Fail 14 Total')
69+
await expect(page.locator('[aria-labelledby=tests]')).toContainText('14 Pass 1 Fail 15 Total')
7070

7171
// unhandled errors
7272
await expect(page.getByTestId('unhandled-errors')).toContainText(
@@ -190,4 +190,23 @@ test.describe('html report', () => {
190190
await expect(annotations.last().getByRole('link')).toHaveAttribute('href', /data\/\w+/)
191191
await expect(annotations.nth(3).getByRole('link')).toHaveAttribute('href', /data\/\w+/)
192192
})
193+
194+
test('visual regression in the report tab', async ({ page }) => {
195+
await page.goto(pageUrl)
196+
197+
await test.step('attachments get processed', async () => {
198+
const item = page.getByLabel('visual regression test')
199+
await item.click({ force: true })
200+
await page.getByTestId('btn-report').click({ force: true })
201+
202+
const artifact = page.getByRole('note')
203+
await expect(artifact).toHaveCount(1)
204+
205+
await expect(artifact.getByRole('heading')).toContainText('Visual Regression')
206+
await expect(artifact).toContainText('fixtures-browser/visual-regression.test.ts:13:3')
207+
await expect(artifact.getByRole('tablist')).toHaveText('Reference')
208+
await expect(artifact.getByRole('tabpanel').getByRole('link')).toHaveAttribute('href', /data\/\w+\.png/)
209+
await expect(artifact.getByRole('tabpanel').getByRole('img')).toHaveAttribute('src', /data\/\w+\.png/)
210+
})
211+
})
193212
})

test/ui/test/ui.spec.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ test.describe('ui', () => {
7070
await page.goto(pageUrl)
7171

7272
// dashboard
73-
await expect(page.locator('[aria-labelledby=tests]')).toContainText('13 Pass 1 Fail 14 Total')
73+
await expect(page.locator('[aria-labelledby=tests]')).toContainText('14 Pass 1 Fail 15 Total')
7474

7575
// unhandled errors
7676
await expect(page.getByTestId('unhandled-errors')).toContainText(
@@ -212,7 +212,7 @@ test.describe('ui', () => {
212212

213213
// match all files when no filter
214214
await page.getByPlaceholder('Search...').fill('')
215-
await page.getByText('PASS (5)').click()
215+
await page.getByText('PASS (6)').click()
216216
await expect(page.getByTestId('details-panel').getByText('fixtures/sample.test.ts', { exact: true })).toBeVisible()
217217

218218
// match nothing
@@ -289,6 +289,25 @@ test.describe('ui', () => {
289289
await expect(page.getByLabel(/fail/i)).not.toBeChecked()
290290
await expect(page.getByLabel(/skip/i)).not.toBeChecked()
291291
})
292+
293+
test('visual regression in the report tab', async ({ page }) => {
294+
await page.goto(pageUrl)
295+
296+
await test.step('attachments get processed', async () => {
297+
const item = page.getByLabel('visual regression test')
298+
await item.click({ force: true })
299+
await page.getByTestId('btn-report').click({ force: true })
300+
301+
const artifact = page.getByRole('note')
302+
await expect(artifact).toHaveCount(1)
303+
304+
await expect(artifact.getByRole('heading')).toContainText('Visual Regression')
305+
await expect(artifact).toContainText('fixtures-browser/visual-regression.test.ts:13:3')
306+
await expect(artifact.getByRole('tablist')).toHaveText('Reference')
307+
await expect(artifact.getByRole('tabpanel').getByRole('link')).toHaveAttribute('href', /__vitest_attachment__\?path=.*?\.png/)
308+
await expect(artifact.getByRole('tabpanel').getByRole('img')).toHaveAttribute('src', /__vitest_attachment__\?path=.*?\.png/)
309+
})
310+
})
292311
})
293312

294313
test.describe('standalone', () => {

test/ui/vitest.config.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1+
import { resolve } from 'node:path'
2+
import { playwright } from '@vitest/browser-playwright'
13
import { defineConfig } from 'vitest/config'
24

35
export default defineConfig({
46
test: {
5-
dir: './fixtures',
6-
environment: 'happy-dom',
77
coverage: {
88
reportOnFailure: true,
99
},
10+
projects: [{
11+
extends: true,
12+
test: {
13+
name: 'fixtures',
14+
dir: './fixtures',
15+
environment: 'happy-dom',
16+
},
17+
}, {
18+
extends: true,
19+
test: {
20+
name: 'browser',
21+
dir: './fixtures-browser',
22+
browser: {
23+
enabled: true,
24+
headless: true,
25+
provider: playwright(),
26+
instances: [{ browser: 'chromium' }],
27+
screenshotFailures: false,
28+
expect: {
29+
toMatchScreenshot: {
30+
resolveScreenshotPath: ({ root, testFileDirectory, arg, ext }) => resolve(root, testFileDirectory, `${arg}${ext}`),
31+
},
32+
},
33+
},
34+
},
35+
}],
1036
},
1137
})

0 commit comments

Comments
 (0)