Skip to content

Commit

Permalink
fix(vitest): show rollup error details as test error (#6686)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Oct 11, 2024
1 parent 4b2ce07 commit 47dde76
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
10 changes: 10 additions & 0 deletions packages/vitest/src/node/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ export function printError(
logger.error(`${e.codeFrame}\n`)
}

if ('__vitest_rollup_error__' in e) {
// https://github.com/vitejs/vite/blob/95020ab49e12d143262859e095025cf02423c1d9/packages/vite/src/node/server/middlewares/error.ts#L25-L36
const err = e.__vitest_rollup_error__ as any
logger.error([
err.plugin && ` Plugin: ${c.magenta(err.plugin)}`,
err.id && ` File: ${c.cyan(err.id)}${err.loc ? `:${err.loc.line}:${err.loc.column}` : ''}`,
err.frame && c.yellow((err.frame as string).split(/\r?\n/g).map(l => ` `.repeat(2) + l).join(`\n`)),
].filter(Boolean).join('\n'))
}

// E.g. AssertionError from assert does not set showDiff but has both actual and expected properties
if (e.diff) {
displayDiff(e.diff, logger.console)
Expand Down
26 changes: 23 additions & 3 deletions packages/vitest/src/node/pools/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(handleRollupError)
const code = result.code
if (!cacheFs || result.externalize) {
return result
Expand Down Expand Up @@ -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(handleRollupError)
},
transform(id, environment) {
return project.vitenode.transformModule(id, environment)
return project.vitenode.transformModule(id, environment).catch(handleRollupError)
},
onPathsCollected(paths) {
ctx.state.collectPaths(paths)
Expand Down Expand Up @@ -104,3 +104,23 @@ 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 && 'plugin' in e) {
// eslint-disable-next-line no-throw-literal
throw {
name: e.name,
message: e.message,
stack: e.stack,
cause: e.cause,
__vitest_rollup_error__: {
plugin: (e as any).plugin,
id: (e as any).id,
loc: (e as any).loc,
frame: (e as any).frame,
},
}
}
throw e
}
1 change: 1 addition & 0 deletions test/config/fixtures/rollup-error/not-found-export.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "vite/no-such-export"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@vitejs/no-such-package'
6 changes: 6 additions & 0 deletions test/config/fixtures/rollup-error/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from "vitest/config";

// pnpm -C test/config test -- --root fixtures/rollup-error --environment happy-dom
// pnpm -C test/config test -- --root fixtures/rollup-error --environment node

export default defineConfig({})
24 changes: 24 additions & 0 deletions test/config/test/rollup-error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'

test('rollup error node', async () => {
const { stdout } = await runVitest({
root: './fixtures/rollup-error',
environment: 'node',
reporters: ['junit'],
})
expect(stdout).toContain(`Error: Missing "./no-such-export" specifier in "vite" package`)
expect(stdout).toContain(`Plugin: vite:import-analysis`)
expect(stdout).toContain(`Error: Failed to load url @vitejs/no-such-package`)
})

test('rollup error web', async () => {
const { stdout } = await runVitest({
root: './fixtures/rollup-error',
environment: 'jsdom',
reporters: ['junit'],
})
expect(stdout).toContain(`Error: Missing "./no-such-export" specifier in "vite" package`)
expect(stdout).toContain(`Plugin: vite:import-analysis`)
expect(stdout).toContain(`Error: Failed to resolve import "@vitejs/no-such-package" from "fixtures/rollup-error/not-found-package.test.ts". Does the file exist?`)
})

0 comments on commit 47dde76

Please sign in to comment.