Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more information about unhandler error #2642

Merged
merged 2 commits into from Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions packages/vitest/src/node/error.ts
Expand Up @@ -66,6 +66,17 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
})
}

const testPath = (e as any).VITEST_TEST_PATH
const testName = (e as any).VITEST_TEST_NAME
// testName has testPath inside
if (testPath && !testName)
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.`))
if (testName) {
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:`
+ '\n- The error was thrown, while Vitest was running this test.'
+ '\n- This was the last recorder test before the error was thrown, if error originated after test finished its execution.'))
}

if (typeof e.cause === 'object' && e.cause && 'name' in e.cause) {
(e.cause as any).name = `Caused by: ${(e.cause as any).name}`
await printError(e.cause, ctx, { fullStack, showCodeFrame: false })
Expand Down Expand Up @@ -97,6 +108,8 @@ const skipErrorProperties = new Set([
'showDiff',
'actual',
'expected',
'VITEST_TEST_NAME',
'VITEST_TEST_PATH',
...Object.getOwnPropertyNames(Error.prototype),
...Object.getOwnPropertyNames(Object.prototype),
])
Expand Down Expand Up @@ -183,10 +196,6 @@ function printStack(

logger.error(color(` ${c.dim(F_POINTER)} ${[frame.method, c.dim(`${path}:${frame.line}:${frame.column}`)].filter(Boolean).join(' ')}`))
onStack?.(frame)

// reached at test file, skip the follow stack
if (frame.file in ctx.state.filesMap)
break
}
logger.error()
const hasProperties = Object.keys(errorProperties).length > 0
Expand Down
15 changes: 11 additions & 4 deletions packages/vitest/src/runtime/worker.ts
@@ -1,7 +1,8 @@
import { resolve } from 'pathe'
import { relative, resolve } from 'pathe'
import { createBirpc } from 'birpc'
import { workerId as poolId } from 'tinypool'
import { ModuleCacheMap } from 'vite-node/client'
import { isPrimitive } from 'vite-node/utils'
import type { ResolvedConfig, WorkerContext, WorkerRPC } from '../types'
import { distDir } from '../constants'
import { getWorkerState } from '../utils'
Expand All @@ -21,6 +22,8 @@ async function startViteNode(ctx: WorkerContext) {
if (_viteNode)
return _viteNode

const { config } = ctx

const processExit = process.exit

process.on('beforeExit', (code) => {
Expand All @@ -33,11 +36,15 @@ async function startViteNode(ctx: WorkerContext) {
}

process.on('unhandledRejection', (err) => {
rpc().onUnhandledRejection(processError(err))
const worker = getWorkerState()
const error = processError(err)
if (worker.filepath && !isPrimitive(error)) {
error.VITEST_TEST_NAME = worker.current?.name
error.VITEST_TEST_PATH = relative(config.root, worker.filepath)
}
rpc().onUnhandledRejection(error)
})

const { config } = ctx

const { run } = (await executeInViteNode({
files: [
resolve(distDir, 'entry.js'),
Expand Down