···77 })
8899 expect(stderr).toContain('throw-unhandled-error.test.ts:9:10')
1010+ expect(stderr).toContain('This error originated in "throw-unhandled-error.test.ts" test file.')
1111+ expect(stderr).toContain('The latest test that might\'ve caused the error is "unhandled exception".')
10121113 if (instances.some(({ browser }) => browser === 'webkit')) {
1214 expect(stderr).toContain('throw-unhandled-error.test.ts:9:20')
+2-2
packages/vitest/src/node/error.ts
···141141 })
142142143143 if (type) {
144144- printErrorType(type, project.ctx)
144144+ printErrorType(type, project.vitest)
145145 }
146146 printErrorMessage(e, logger)
147147 if (options.screenshotPaths?.length) {
···205205 logger.error(
206206 c.red(
207207 `This error originated in "${c.bold(
208208- testPath,
208208+ 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 )
+4-4
packages/vitest/src/runtime/execute.ts
···66import { pathToFileURL } from 'node:url'
77import vm from 'node:vm'
88import { processError } from '@vitest/utils/error'
99-import { normalize, relative } from 'pathe'
99+import { normalize } from 'pathe'
1010import { DEFAULT_REQUEST_STUBS, ViteNodeRunner } from 'vite-node/client'
1111import {
1212 isInternalRequest,
···5959 const worker = state()
60606161 // if error happens during a test
6262- if (worker.current) {
6262+ 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
···70707171 const error = processError(err)
7272 if (!isPrimitive(error)) {
7373- error.VITEST_TEST_NAME = worker.current?.name
7373+ error.VITEST_TEST_NAME = worker.current?.type === 'test' ? worker.current.name : undefined
7474 if (worker.filepath) {
7575- error.VITEST_TEST_PATH = relative(state().config.root, worker.filepath)
7575+ error.VITEST_TEST_PATH = worker.filepath
7676 }
7777 error.VITEST_AFTER_ENV_TEARDOWN = worker.environmentTeardownRun
7878 }
···44 _fake: never
55}
6677-test('unhandled exception', () => {
77+test('unhandled exception', async () => {
88 ;(async () => {
99 throw new Error('custom_unhandled_error')
1010 })()
1111+ // trigger the error during the test so the report includes the helpful message
1212+ // in reality, most tests will have something going on here already
1313+ await new Promise<void>((resolve) => setTimeout(() => resolve(), 50))
1114})