Skip to content

Commit

Permalink
fix(browser): print related test file and potential test in unhandled…
Browse files Browse the repository at this point in the history
… errors (#7564)
  • Loading branch information
sheremet-va authored Feb 27, 2025
1 parent d5cb821 commit fee90d8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
10 changes: 10 additions & 0 deletions packages/browser/src/client/public/error-catcher.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import { channel, client } from '@vitest/browser/client'

function serializeError(unhandledError) {
const state = globalThis.__vitest_worker__
const VITEST_TEST_NAME = state && state.current && state.current.type === 'test'
? state.current.name
: undefined
const VITEST_TEST_PATH = state && state.filepath ? state.filepath : undefined

if (typeof unhandledError !== 'object' || !unhandledError) {
return {
message: String(unhandledError),
VITEST_TEST_NAME,
VITEST_TEST_PATH,
}
}

return {
name: unhandledError.name,
message: unhandledError.message,
stack: String(unhandledError.stack),
VITEST_TEST_NAME,
VITEST_TEST_PATH,
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function printErrorInner(
})

if (type) {
printErrorType(type, project.ctx)
printErrorType(type, project.vitest)
}
printErrorMessage(e, logger)
if (options.screenshotPaths?.length) {
Expand Down Expand Up @@ -205,7 +205,7 @@ function printErrorInner(
logger.error(
c.red(
`This error originated in "${c.bold(
testPath,
relative(project.config.root, testPath),
)}" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.`,
),
)
Expand Down
8 changes: 4 additions & 4 deletions packages/vitest/src/runtime/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fs from 'node:fs'
import { pathToFileURL } from 'node:url'
import vm from 'node:vm'
import { processError } from '@vitest/utils/error'
import { normalize, relative } from 'pathe'
import { normalize } from 'pathe'
import { DEFAULT_REQUEST_STUBS, ViteNodeRunner } from 'vite-node/client'
import {
isInternalRequest,
Expand Down Expand Up @@ -59,7 +59,7 @@ function listenForErrors(state: () => WorkerGlobalState) {
const worker = state()

// if error happens during a test
if (worker.current) {
if (worker.current?.type === 'test') {
const listeners = process.listeners(event as 'uncaughtException')
// if there is another listener, assume that it's handled by user code
// one is Vitest's own listener
Expand All @@ -70,9 +70,9 @@ function listenForErrors(state: () => WorkerGlobalState) {

const error = processError(err)
if (!isPrimitive(error)) {
error.VITEST_TEST_NAME = worker.current?.name
error.VITEST_TEST_NAME = worker.current?.type === 'test' ? worker.current.name : undefined
if (worker.filepath) {
error.VITEST_TEST_PATH = relative(state().config.root, worker.filepath)
error.VITEST_TEST_PATH = worker.filepath
}
error.VITEST_AFTER_ENV_TEARDOWN = worker.environmentTeardownRun
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ interface _Unused {
_fake: never
}

test('unhandled exception', () => {
test('unhandled exception', async () => {
;(async () => {
throw new Error('custom_unhandled_error')
})()
// trigger the error during the test so the report includes the helpful message
// in reality, most tests will have something going on here already
await new Promise<void>((resolve) => setTimeout(() => resolve(), 50))
})
2 changes: 2 additions & 0 deletions test/browser/specs/unhandled.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ test('prints correct unhandled error stack', async () => {
})

expect(stderr).toContain('throw-unhandled-error.test.ts:9:10')
expect(stderr).toContain('This error originated in "throw-unhandled-error.test.ts" test file.')
expect(stderr).toContain('The latest test that might\'ve caused the error is "unhandled exception".')

if (instances.some(({ browser }) => browser === 'webkit')) {
expect(stderr).toContain('throw-unhandled-error.test.ts:9:20')
Expand Down

0 comments on commit fee90d8

Please sign in to comment.