Skip to content

Commit 1ffb0ef

Browse files
authoredJan 11, 2023
feat: add more information about unhandler error (#2642)
* feat: add more information about unhandler error * chore: fix type issue
1 parent 866f449 commit 1ffb0ef

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed
 

‎packages/vitest/src/node/error.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
6666
})
6767
}
6868

69+
const testPath = (e as any).VITEST_TEST_PATH
70+
const testName = (e as any).VITEST_TEST_NAME
71+
// testName has testPath inside
72+
if (testPath && !testName)
73+
ctx.logger.error(c.red(`This error originated in "${c.bold(testPath)}" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.`))
74+
if (testName) {
75+
ctx.logger.error(c.red(`The latest test that migh've cause the error is "${c.bold(testName)}". It might mean one of the following:`
76+
+ '\n- The error was thrown, while Vitest was running this test.'
77+
+ '\n- This was the last recorder test before the error was thrown, if error originated after test finished its execution.'))
78+
}
79+
6980
if (typeof e.cause === 'object' && e.cause && 'name' in e.cause) {
7081
(e.cause as any).name = `Caused by: ${(e.cause as any).name}`
7182
await printError(e.cause, ctx, { fullStack, showCodeFrame: false })
@@ -97,6 +108,8 @@ const skipErrorProperties = new Set([
97108
'showDiff',
98109
'actual',
99110
'expected',
111+
'VITEST_TEST_NAME',
112+
'VITEST_TEST_PATH',
100113
...Object.getOwnPropertyNames(Error.prototype),
101114
...Object.getOwnPropertyNames(Object.prototype),
102115
])
@@ -183,10 +196,6 @@ function printStack(
183196

184197
logger.error(color(` ${c.dim(F_POINTER)} ${[frame.method, c.dim(`${path}:${frame.line}:${frame.column}`)].filter(Boolean).join(' ')}`))
185198
onStack?.(frame)
186-
187-
// reached at test file, skip the follow stack
188-
if (frame.file in ctx.state.filesMap)
189-
break
190199
}
191200
logger.error()
192201
const hasProperties = Object.keys(errorProperties).length > 0

‎packages/vitest/src/runtime/worker.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { resolve } from 'pathe'
1+
import { relative, resolve } from 'pathe'
22
import { createBirpc } from 'birpc'
33
import { workerId as poolId } from 'tinypool'
44
import { ModuleCacheMap } from 'vite-node/client'
5+
import { isPrimitive } from 'vite-node/utils'
56
import type { ResolvedConfig, WorkerContext, WorkerRPC } from '../types'
67
import { distDir } from '../constants'
78
import { getWorkerState } from '../utils'
@@ -21,6 +22,8 @@ async function startViteNode(ctx: WorkerContext) {
2122
if (_viteNode)
2223
return _viteNode
2324

25+
const { config } = ctx
26+
2427
const processExit = process.exit
2528

2629
process.exit = (code = process.exitCode || 0): never => {
@@ -30,11 +33,15 @@ async function startViteNode(ctx: WorkerContext) {
3033
}
3134

3235
process.on('unhandledRejection', (err) => {
33-
rpc().onUnhandledRejection(processError(err))
36+
const worker = getWorkerState()
37+
const error = processError(err)
38+
if (worker.filepath && !isPrimitive(error)) {
39+
error.VITEST_TEST_NAME = worker.current?.name
40+
error.VITEST_TEST_PATH = relative(config.root, worker.filepath)
41+
}
42+
rpc().onUnhandledRejection(error)
3443
})
3544

36-
const { config } = ctx
37-
3845
const { run } = (await executeInViteNode({
3946
files: [
4047
resolve(distDir, 'entry.js'),

0 commit comments

Comments
 (0)
Please sign in to comment.