Skip to content

Commit 80ce0e1

Browse files
authoredJan 15, 2025··
feat(api): add onBrowserInit event (#7255)
1 parent 003c0be commit 80ce0e1

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
lines changed
 

‎docs/advanced/api/reporters.md

+8
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ export default new MyReporter()
8181
```
8282
:::
8383

84+
## onBrowserInit <Badge type="warning">experimental</Badge> {#onbrowserinit}
85+
86+
```ts
87+
function onBrowserInit(project: TestProject): Awaitable<void>
88+
```
89+
90+
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.
91+
8492
## onTestRunStart
8593

8694
```ts

‎packages/vitest/src/node/project.ts

+1
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ export class TestProject {
531531

532532
if (!this.browser && this._parent?._parentBrowser) {
533533
this.browser = this._parent._parentBrowser.spawn(this)
534+
await this.vitest.report('onBrowserInit', this)
534535
}
535536
})
536537

‎packages/vitest/src/node/types/reporter.ts

+7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ import type { SerializedError } from '@vitest/utils'
33
import type { SerializedTestSpecification } from '../../runtime/types/utils'
44
import type { Awaitable, UserConsoleLog } from '../../types/general'
55
import type { Vitest } from '../core'
6+
import type { TestProject } from '../project'
67
import type { ReportedHookContext, TestCase, TestModule, TestSuite } from '../reporters/reported-tasks'
78
import type { TestSpecification } from '../spec'
89

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

1112
export interface Reporter {
1213
onInit?: (vitest: Vitest) => void
14+
/**
15+
* Called when the project initiated the browser instance.
16+
* project.browser will always be defined.
17+
* @experimental
18+
*/
19+
onBrowserInit?: (project: TestProject) => Awaitable<void>
1320
/**
1421
* @deprecated use `onTestRunStart` instead
1522
*/

‎test/browser/specs/runner.test.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,45 @@ import { readFile } from 'node:fs/promises'
44
import { beforeAll, describe, expect, onTestFailed, test } from 'vitest'
55
import { instances, provider, runBrowserTests } from './utils'
66

7+
function noop() {}
8+
79
describe('running browser tests', async () => {
810
let stderr: string
911
let stdout: string
1012
let browserResultJson: JsonTestResults
1113
let passedTests: any[]
1214
let failedTests: any[]
1315
let vitest: Vitest
16+
const events: string[] = []
1417

1518
beforeAll(async () => {
1619
({
1720
stderr,
1821
stdout,
1922
ctx: vitest,
20-
} = await runBrowserTests())
23+
} = await runBrowserTests({
24+
reporters: [
25+
{
26+
onBrowserInit(project) {
27+
events.push(`onBrowserInit ${project.name}`)
28+
},
29+
},
30+
'json',
31+
{
32+
onInit: noop,
33+
onPathsCollected: noop,
34+
onCollected: noop,
35+
onFinished: noop,
36+
onTaskUpdate: noop,
37+
onTestRemoved: noop,
38+
onWatcherStart: noop,
39+
onWatcherRerun: noop,
40+
onServerRestart: noop,
41+
onUserConsoleLog: noop,
42+
},
43+
'default',
44+
],
45+
}))
2146

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

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

62+
vitest.projects.forEach((project) => {
63+
// the order is non-deterministic
64+
expect(events).toContain(`onBrowserInit ${project.name}`)
65+
})
66+
3767
// test files are optimized automatically
3868
expect(vitest.projects.map(p => p.browser?.vite.config.optimizeDeps.entries))
3969
.toEqual(vitest.projects.map(() => expect.arrayContaining(testFiles)))

‎test/browser/vitest.config.mts

-14
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { defineConfig } from 'vitest/config'
66

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

9-
function noop() {}
10-
119
const provider = process.env.PROVIDER || 'playwright'
1210
const browser = process.env.BROWSER || (provider === 'playwright' ? 'chromium' : 'chrome')
1311

@@ -106,18 +104,6 @@ export default defineConfig({
106104
html: './html/index.html',
107105
json: './browser.json',
108106
},
109-
reporters: ['json', {
110-
onInit: noop,
111-
onPathsCollected: noop,
112-
onCollected: noop,
113-
onFinished: noop,
114-
onTaskUpdate: noop,
115-
onTestRemoved: noop,
116-
onWatcherStart: noop,
117-
onWatcherRerun: noop,
118-
onServerRestart: noop,
119-
onUserConsoleLog: noop,
120-
}, 'default'],
121107
env: {
122108
BROWSER: browser,
123109
},

0 commit comments

Comments
 (0)
Please sign in to comment.