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

fix(browser): throw an error if test failed to load #3390

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/vitest/src/node/browser/playwright.ts
Expand Up @@ -2,6 +2,7 @@ import type { Page } from 'playwright'
import type { BrowserProvider, BrowserProviderOptions } from '../../types/browser'
import { ensurePackageInstalled } from '../pkg'
import type { WorkspaceProject } from '../workspace'
import type { Awaitable } from '../../types'

export const playwrightBrowsers = ['firefox', 'webkit', 'chromium'] as const
export type PlaywrightBrowser = typeof playwrightBrowsers[number]
Expand Down Expand Up @@ -49,6 +50,13 @@ export class PlaywrightBrowserProvider implements BrowserProvider {
return this.cachedBrowser
}

catchError(cb: (error: Error) => Awaitable<void>) {
this.cachedBrowser?.on('pageerror', cb)
return () => {
this.cachedBrowser?.off('pageerror', cb)
}
}

async openPage(url: string) {
const browserInstance = await this.openBrowser()
await browserInstance.goto(url)
Expand Down
6 changes: 6 additions & 0 deletions packages/vitest/src/node/browser/webdriver.ts
@@ -1,4 +1,5 @@
import type { Browser } from 'webdriverio'
import type { Awaitable } from '@vitest/utils'
import type { BrowserProvider, BrowserProviderOptions } from '../../types/browser'
import { ensurePackageInstalled } from '../pkg'
import type { WorkspaceProject } from '../workspace'
Expand Down Expand Up @@ -70,6 +71,11 @@ export class WebdriverBrowserProvider implements BrowserProvider {
await browserInstance.url(url)
}

// TODO
catchError(_cb: (error: Error) => Awaitable<void>) {
return () => {}
}

async close() {
await Promise.all([
this.stopSafari(),
Expand Down
21 changes: 17 additions & 4 deletions packages/vitest/src/node/pools/browser.ts
Expand Up @@ -8,10 +8,23 @@ import type { BrowserProvider } from '../../types/browser'
export function createBrowserPool(ctx: Vitest): ProcessPool {
const providers = new Set<BrowserProvider>()

const waitForTest = (id: string) => {
const waitForTest = async (provider: BrowserProvider, id: string) => {
const defer = createDefer()
ctx.state.browserTestPromises.set(id, defer)
return defer
const off = provider.catchError((error) => {
if (id !== 'no-isolate') {
Object.defineProperty(error, 'VITEST_TEST_PATH', {
value: id,
})
}
defer.reject(error)
})
try {
return await defer
}
finally {
off()
}
}

const runTests = async (project: WorkspaceProject, files: string[]) => {
Expand Down Expand Up @@ -40,15 +53,15 @@ export function createBrowserPool(ctx: Vitest): ProcessPool {
url.searchParams.append('path', path)
url.searchParams.set('id', path)
await provider.openPage(url.toString())
await waitForTest(path)
await waitForTest(provider, path)
}
}
else {
const url = new URL('/', origin)
url.searchParams.set('id', 'no-isolate')
paths.forEach(path => url.searchParams.append('path', path))
await provider.openPage(url.toString())
await waitForTest('no-isolate')
await waitForTest(provider, 'no-isolate')
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/types/browser.ts
Expand Up @@ -11,6 +11,7 @@ export interface BrowserProvider {
getSupportedBrowsers(): readonly string[]
initialize(ctx: WorkspaceProject, options: BrowserProviderOptions): Awaitable<void>
openPage(url: string): Awaitable<void>
catchError(cb: (error: Error) => Awaitable<void>): () => Awaitable<void>
close(): Awaitable<void>
}

Expand Down