Skip to content

Commit

Permalink
feat(api): add onBrowserInit event (#7255)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Jan 15, 2025
1 parent 003c0be commit 80ce0e1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
8 changes: 8 additions & 0 deletions docs/advanced/api/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ export default new MyReporter()
```
:::

## onBrowserInit <Badge type="warning">experimental</Badge> {#onbrowserinit}

```ts
function onBrowserInit(project: TestProject): Awaitable<void>
```

This method is called when the browser instance is initiated. It receives an instance of the project for which the browser is initiated. `project.browser` will always be defined when this method is called.

## onTestRunStart

```ts
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ export class TestProject {

if (!this.browser && this._parent?._parentBrowser) {
this.browser = this._parent._parentBrowser.spawn(this)
await this.vitest.report('onBrowserInit', this)
}
})

Expand Down
7 changes: 7 additions & 0 deletions packages/vitest/src/node/types/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ import type { SerializedError } from '@vitest/utils'
import type { SerializedTestSpecification } from '../../runtime/types/utils'
import type { Awaitable, UserConsoleLog } from '../../types/general'
import type { Vitest } from '../core'
import type { TestProject } from '../project'
import type { ReportedHookContext, TestCase, TestModule, TestSuite } from '../reporters/reported-tasks'
import type { TestSpecification } from '../spec'

export type TestRunEndReason = 'passed' | 'interrupted' | 'failed'

export interface Reporter {
onInit?: (vitest: Vitest) => void
/**
* Called when the project initiated the browser instance.
* project.browser will always be defined.
* @experimental
*/
onBrowserInit?: (project: TestProject) => Awaitable<void>
/**
* @deprecated use `onTestRunStart` instead
*/
Expand Down
32 changes: 31 additions & 1 deletion test/browser/specs/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,45 @@ import { readFile } from 'node:fs/promises'
import { beforeAll, describe, expect, onTestFailed, test } from 'vitest'
import { instances, provider, runBrowserTests } from './utils'

function noop() {}

describe('running browser tests', async () => {
let stderr: string
let stdout: string
let browserResultJson: JsonTestResults
let passedTests: any[]
let failedTests: any[]
let vitest: Vitest
const events: string[] = []

beforeAll(async () => {
({
stderr,
stdout,
ctx: vitest,
} = await runBrowserTests())
} = await runBrowserTests({
reporters: [
{
onBrowserInit(project) {
events.push(`onBrowserInit ${project.name}`)
},
},
'json',
{
onInit: noop,
onPathsCollected: noop,
onCollected: noop,
onFinished: noop,
onTaskUpdate: noop,
onTestRemoved: noop,
onWatcherStart: noop,
onWatcherRerun: noop,
onServerRestart: noop,
onUserConsoleLog: noop,
},
'default',
],
}))

const browserResult = await readFile('./browser.json', 'utf-8')
browserResultJson = JSON.parse(browserResult)
Expand All @@ -34,6 +59,11 @@ describe('running browser tests', async () => {

const testFiles = browserResultJson.testResults.map(t => t.name)

vitest.projects.forEach((project) => {
// the order is non-deterministic
expect(events).toContain(`onBrowserInit ${project.name}`)
})

// test files are optimized automatically
expect(vitest.projects.map(p => p.browser?.vite.config.optimizeDeps.entries))
.toEqual(vitest.projects.map(() => expect.arrayContaining(testFiles)))
Expand Down
14 changes: 0 additions & 14 deletions test/browser/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { defineConfig } from 'vitest/config'

const dir = dirname(fileURLToPath(import.meta.url))

function noop() {}

const provider = process.env.PROVIDER || 'playwright'
const browser = process.env.BROWSER || (provider === 'playwright' ? 'chromium' : 'chrome')

Expand Down Expand Up @@ -106,18 +104,6 @@ export default defineConfig({
html: './html/index.html',
json: './browser.json',
},
reporters: ['json', {
onInit: noop,
onPathsCollected: noop,
onCollected: noop,
onFinished: noop,
onTaskUpdate: noop,
onTestRemoved: noop,
onWatcherStart: noop,
onWatcherRerun: noop,
onServerRestart: noop,
onUserConsoleLog: noop,
}, 'default'],
env: {
BROWSER: browser,
},
Expand Down

0 comments on commit 80ce0e1

Please sign in to comment.