Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(browser): add an option to take screenshots if the browser test fails #5975

Merged
merged 19 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 14 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,20 @@ Should Vitest UI be injected into the page. By default, injects UI iframe during

Default iframe's viewport.

#### browser.screenshotDirectory {#browser-screenshotdirectory}

- **Type:** `string`
- **Default:** `__snapshots__` in the test file directory

Path to the snapshots directory relative to the `root`.

#### browser.screenshotFailures {#browser-screenshotfailures}

- **Type:** `boolean`
- **Default:** `!browser.ui`

Should Vitest take screenshots if the test fails.

#### browser.orchestratorScripts {#browser-orchestratorscripts}

- **Type:** `BrowserScript[]`
Expand Down
7 changes: 7 additions & 0 deletions packages/browser/src/client/tester/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { VitestExecutor } from 'vitest/execute'
import { NodeBenchmarkRunner, VitestTestRunner } from 'vitest/runners'
import { loadDiffConfig, loadSnapshotSerializers, takeCoverageInsideWorker } from 'vitest/browser'
import { TraceMap, originalPositionFor } from 'vitest/utils'
import { page } from '@vitest/browser/context'
import { importId } from '../utils'
import { globalChannel } from '../channel'
import { VitestBrowserSnapshotEnvironment } from './snapshot'
Expand Down Expand Up @@ -53,6 +54,12 @@ export function createBrowserRunner(
}
}

onTaskFinished = async (task: Task) => {
if (this.config.browser.screenshotFailures && task.result?.state === 'fail') {
await page.screenshot()
userquin marked this conversation as resolved.
Show resolved Hide resolved
}
}

onCancel = (reason: CancelReason) => {
super.onCancel?.(reason)
globalChannel.postMessage({ type: 'cancel', reason })
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/src/client/tester/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ async function runTests(files: string[]) {
try {
preparedData = await prepareTestEnvironment(files)
}
catch (error) {
debug('data cannot be loaded because it threw an error')
catch (error: any) {
debug('data cannot be loaded because it threw an error', error.stack || error.message)
await client.rpc.onUnhandledError(serializeError(error), 'Preload Error')
done(files)
return
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/client/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default defineConfig({
orchestrator: resolve(__dirname, './orchestrator.html'),
tester: resolve(__dirname, './tester/tester.html'),
},
external: [/__virtual_vitest__/],
external: [/__virtual_vitest__/, '@vitest/browser/context'],
},
},
plugins: [
Expand Down
7 changes: 7 additions & 0 deletions packages/runner/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ export async function runTest(test: Test | Custom, runner: VitestRunner) {
return
}

try {
await runner.onTaskFinished?.(test)
}
catch (e) {
failTask(test.result, e, runner.config.diffOptions)
}

try {
await callSuiteHook(suite, test, 'afterEach', runner, [
test.context,
Expand Down
4 changes: 4 additions & 0 deletions packages/runner/src/types/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export interface VitestRunner {
test: Task,
options: { retry: number; repeats: number }
) => unknown
/**
* When the task has finished running, but before cleanup hooks are called
*/
onTaskFinished?: (test: Task) => unknown
/**
* Called after result and state are set.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ export function resolveConfig(
resolved.browser.screenshotDirectory,
)
}
resolved.browser.screenshotFailures ??= !resolved.browser.ui

resolved.browser.viewport ??= {} as any
resolved.browser.viewport.width ??= 414
Expand Down
7 changes: 7 additions & 0 deletions packages/vitest/src/types/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ export interface BrowserConfigOptions {
* @default __screenshots__
*/
screenshotDirectory?: string

/**
* Should Vitest take screenshots if the test fails
* @default !browser.ui
*/
screenshotFailures?: boolean

/**
* Scripts injected into the tester iframe.
*/
Expand Down
Loading