Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vitest): show rollup error details as test error
Browse files Browse the repository at this point in the history
hi-ogawa committed Oct 11, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 11b9432 commit 201121b
Showing 2 changed files with 30 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/vitest/src/node/error.ts
Original file line number Diff line number Diff line change
@@ -119,6 +119,12 @@ export function printError(
logger.error(`${e.codeFrame}\n`)
}

if ('__vitest_rollup_error__' in e) {
// TODO: format like vite?
const r = e.__vitest_rollup_error__ as any
logger.error(r)
}

// E.g. AssertionError from assert does not set showDiff but has both actual and expected properties
if (e.diff) {
displayDiff(e.diff, logger.console)
27 changes: 24 additions & 3 deletions packages/vitest/src/node/pools/rpc.ts
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ export function createMethodsRPC(project: WorkspaceProject, options: MethodsOpti
return r?.map as RawSourceMap | undefined
},
async fetch(id, transformMode) {
const result = await project.vitenode.fetchResult(id, transformMode)
const result = await project.vitenode.fetchResult(id, transformMode).catch(e => handleRollupError(e))
const code = result.code
if (!cacheFs || result.externalize) {
return result
@@ -66,10 +66,10 @@ export function createMethodsRPC(project: WorkspaceProject, options: MethodsOpti
return { id: tmp }
},
resolveId(id, importer, transformMode) {
return project.vitenode.resolveId(id, importer, transformMode)
return project.vitenode.resolveId(id, importer, transformMode).catch(e => handleRollupError(e))
},
transform(id, environment) {
return project.vitenode.transformModule(id, environment)
return project.vitenode.transformModule(id, environment).catch(e => handleRollupError(e))
},
onPathsCollected(paths) {
ctx.state.collectPaths(paths)
@@ -104,3 +104,24 @@ export function createMethodsRPC(project: WorkspaceProject, options: MethodsOpti
},
}
}

// serialize rollup error on server to preserve details as a test error
function handleRollupError(e: unknown): never {
if (e instanceof Error && 'loc' in e && e.loc && typeof e.loc === 'object') {
// eslint-disable-next-line no-throw-literal
throw {
name: e.name,
message: e.message,
stack: e.stack,
cause: e.cause,
__vitest_rollup_error__: {
id: (e as any).id,
loc: (e as any).loc,
frame: (e as any).frame,
plugin: (e as any).plugin,
pluginCode: (e as any).pluginCode,
},
}
}
throw e
}

0 comments on commit 201121b

Please sign in to comment.