Skip to content

Commit

Permalink
fix(browser): throw an error if test failed to load (#3390)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored May 17, 2023
1 parent 2cb1a15 commit b354bc1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
8 changes: 8 additions & 0 deletions packages/vitest/src/node/browser/playwright.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit b354bc1

Please sign in to comment.