Skip to content

Commit fee90d8

Browse files
authored
fix(browser): print related test file and potential test in unhandled errors (#7564)
1 parent d5cb821 commit fee90d8

File tree

5 files changed

+22
-7
lines changed

5 files changed

+22
-7
lines changed

packages/browser/src/client/public/error-catcher.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
import { channel, client } from '@vitest/browser/client'
22

33
function serializeError(unhandledError) {
4+
const state = globalThis.__vitest_worker__
5+
const VITEST_TEST_NAME = state && state.current && state.current.type === 'test'
6+
? state.current.name
7+
: undefined
8+
const VITEST_TEST_PATH = state && state.filepath ? state.filepath : undefined
9+
410
if (typeof unhandledError !== 'object' || !unhandledError) {
511
return {
612
message: String(unhandledError),
13+
VITEST_TEST_NAME,
14+
VITEST_TEST_PATH,
715
}
816
}
917

1018
return {
1119
name: unhandledError.name,
1220
message: unhandledError.message,
1321
stack: String(unhandledError.stack),
22+
VITEST_TEST_NAME,
23+
VITEST_TEST_PATH,
1424
}
1525
}
1626

packages/vitest/src/node/error.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function printErrorInner(
141141
})
142142

143143
if (type) {
144-
printErrorType(type, project.ctx)
144+
printErrorType(type, project.vitest)
145145
}
146146
printErrorMessage(e, logger)
147147
if (options.screenshotPaths?.length) {
@@ -205,7 +205,7 @@ function printErrorInner(
205205
logger.error(
206206
c.red(
207207
`This error originated in "${c.bold(
208-
testPath,
208+
relative(project.config.root, testPath),
209209
)}" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.`,
210210
),
211211
)

packages/vitest/src/runtime/execute.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fs from 'node:fs'
66
import { pathToFileURL } from 'node:url'
77
import vm from 'node:vm'
88
import { processError } from '@vitest/utils/error'
9-
import { normalize, relative } from 'pathe'
9+
import { normalize } from 'pathe'
1010
import { DEFAULT_REQUEST_STUBS, ViteNodeRunner } from 'vite-node/client'
1111
import {
1212
isInternalRequest,
@@ -59,7 +59,7 @@ function listenForErrors(state: () => WorkerGlobalState) {
5959
const worker = state()
6060

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

7171
const error = processError(err)
7272
if (!isPrimitive(error)) {
73-
error.VITEST_TEST_NAME = worker.current?.name
73+
error.VITEST_TEST_NAME = worker.current?.type === 'test' ? worker.current.name : undefined
7474
if (worker.filepath) {
75-
error.VITEST_TEST_PATH = relative(state().config.root, worker.filepath)
75+
error.VITEST_TEST_PATH = worker.filepath
7676
}
7777
error.VITEST_AFTER_ENV_TEARDOWN = worker.environmentTeardownRun
7878
}

test/browser/fixtures/unhandled/throw-unhandled-error.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ interface _Unused {
44
_fake: never
55
}
66

7-
test('unhandled exception', () => {
7+
test('unhandled exception', async () => {
88
;(async () => {
99
throw new Error('custom_unhandled_error')
1010
})()
11+
// trigger the error during the test so the report includes the helpful message
12+
// in reality, most tests will have something going on here already
13+
await new Promise<void>((resolve) => setTimeout(() => resolve(), 50))
1114
})

test/browser/specs/unhandled.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ test('prints correct unhandled error stack', async () => {
77
})
88

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

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy