Skip to content

Commit

Permalink
fix(browser): print correct stack trace for unhandled errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jul 15, 2024
1 parent 3826941 commit 44cabb0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/browser/src/node/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { WebSocket } from 'ws'
import { WebSocketServer } from 'ws'
import type { BrowserCommandContext } from 'vitest/node'
import { createDebugger, isFileServingAllowed } from 'vitest/node'
import type { ErrorWithDiff } from 'vitest'
import type { WebSocketBrowserEvents, WebSocketBrowserHandlers } from './types'
import type { BrowserServer } from './server'
import { resolveMock } from './resolveMock'
Expand Down Expand Up @@ -66,6 +67,10 @@ export function setupBrowserRpc(
const rpc = createBirpc<WebSocketBrowserEvents, WebSocketBrowserHandlers>(
{
async onUnhandledError(error, type) {
if (error && typeof error === 'object') {
const _error = error as ErrorWithDiff
_error.stacks = server.parseErrorStacktrace(_error)
}
ctx.state.catchError(error, type)
},
async onCollected(files) {
Expand Down
11 changes: 11 additions & 0 deletions test/browser/fixtures/unhandled/throw-unhandled-error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { test } from 'vitest';

interface _Unused {
_fake: never
}

test('unhandled exception', () => {
;(async () => {
throw new Error('custom_unhandled_error')
})()
})
18 changes: 18 additions & 0 deletions test/browser/fixtures/unhandled/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { fileURLToPath } from 'node:url'
import { defineConfig } from 'vitest/config'

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

export default defineConfig({
cacheDir: fileURLToPath(new URL("./node_modules/.vite", import.meta.url)),
test: {
browser: {
enabled: true,
provider,
name,
headless: true,
},
},
})
15 changes: 15 additions & 0 deletions test/browser/specs/unhandled.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect, test } from 'vitest'
import { runBrowserTests } from './utils'

test('prints correct unhandled error stack', async () => {
const { stderr, browser } = await runBrowserTests({
root: './fixtures/unhandled',
})

if (browser === 'webkit') {
expect(stderr).toContain('throw-unhandled-error.test.ts:9:20')
}
else {
expect(stderr).toContain('throw-unhandled-error.test.ts:9:10')

Check failure on line 13 in test/browser/specs/unhandled.test.ts

View workflow job for this annotation

GitHub Actions / test-browser-windows (chrome, chromium)

specs/unhandled.test.ts > prints correct unhandled error stack

AssertionError: expected '' to contain 'throw-unhandled-error.test.ts:9:10' - Expected + Received - throw-unhandled-error.test.ts:9:10 ❯ specs/unhandled.test.ts:13:20

Check failure on line 13 in test/browser/specs/unhandled.test.ts

View workflow job for this annotation

GitHub Actions / test-browser-windows (edge, webkit)

specs/unhandled.test.ts > prints correct unhandled error stack

AssertionError: expected '' to contain 'throw-unhandled-error.test.ts:9:10' - Expected + Received - throw-unhandled-error.test.ts:9:10 ❯ specs/unhandled.test.ts:13:20
}
})

0 comments on commit 44cabb0

Please sign in to comment.