Skip to content

Commit b298df6

Browse files
authored
fix(vitest): render non-Error causes from env setup so the actual diagnostic is not dropped (#10567)
1 parent 032d318 commit b298df6

3 files changed

Lines changed: 105 additions & 3 deletions

File tree

packages/vitest/src/node/printError.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,19 @@ function printErrorInner(
243243
)
244244
}
245245

246-
if (typeof e.cause === 'object' && e.cause && 'name' in e.cause) {
247-
(e.cause as any).name = `Caused by: ${(e.cause as any).name}`
248-
printErrorInner(e.cause, project, {
246+
if (e.cause != null) {
247+
let cause: any = e.cause
248+
if (typeof cause !== 'object' || cause === null) {
249+
const causeStr = String(cause)
250+
cause = { name: 'Caused by', message: causeStr, stack: causeStr }
251+
}
252+
else if (!('name' in cause)) {
253+
cause = { ...cause, name: 'Caused by' }
254+
}
255+
else {
256+
cause.name = `Caused by: ${cause.name}`
257+
}
258+
printErrorInner(cause, project, {
249259
showCodeFrame: false,
250260
logger: options.logger,
251261
parseErrorStacktrace: options.parseErrorStacktrace,
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { expect, test } from 'vitest'
2+
import { runInlineTests } from '../../test-utils'
3+
4+
test('non-Error string thrown from env setup surfaces in stderr', async () => {
5+
const { stderr } = await runInlineTests({
6+
'throwing-env.js': `
7+
export default {
8+
name: 'throwing-env',
9+
transformMode: 'ssr',
10+
async setup() {
11+
throw 'the real reason setup failed (a non-Error value)'
12+
},
13+
}
14+
`,
15+
'vitest.config.js': `
16+
import { defineConfig } from 'vitest/config'
17+
export default defineConfig({
18+
test: {
19+
environment: './throwing-env.js',
20+
},
21+
})
22+
`,
23+
'example.test.js': `
24+
import { test, expect } from 'vitest'
25+
test('noop', () => { expect(1).toBe(1) })
26+
`,
27+
})
28+
29+
expect(stderr).toContain('Failed to start')
30+
expect(stderr).toContain('the real reason setup failed (a non-Error value)')
31+
})
32+
33+
test('non-Error plain object thrown from env setup surfaces in stderr', async () => {
34+
const { stderr } = await runInlineTests({
35+
'throwing-env.js': `
36+
export default {
37+
name: 'throwing-env',
38+
transformMode: 'ssr',
39+
async setup() {
40+
throw { reason: 'wasm-init-failed', code: 42 }
41+
},
42+
}
43+
`,
44+
'vitest.config.js': `
45+
import { defineConfig } from 'vitest/config'
46+
export default defineConfig({
47+
test: {
48+
environment: './throwing-env.js',
49+
},
50+
})
51+
`,
52+
'example.test.js': `
53+
import { test, expect } from 'vitest'
54+
test('noop', () => { expect(1).toBe(1) })
55+
`,
56+
})
57+
58+
expect(stderr).toContain('Failed to start')
59+
expect(stderr).toMatch(/wasm-init-failed|reason/)
60+
})
61+
62+
test('Error thrown from env setup still renders with Caused by prefix', async () => {
63+
const { stderr } = await runInlineTests({
64+
'throwing-env.js': `
65+
export default {
66+
name: 'throwing-env',
67+
transformMode: 'ssr',
68+
async setup() {
69+
throw new Error('explicit error reason')
70+
},
71+
}
72+
`,
73+
'vitest.config.js': `
74+
import { defineConfig } from 'vitest/config'
75+
export default defineConfig({
76+
test: {
77+
environment: './throwing-env.js',
78+
},
79+
})
80+
`,
81+
'example.test.js': `
82+
import { test, expect } from 'vitest'
83+
test('noop', () => { expect(1).toBe(1) })
84+
`,
85+
})
86+
87+
expect(stderr).toContain('Failed to start')
88+
expect(stderr).toContain('Caused by')
89+
expect(stderr).toContain('explicit error reason')
90+
})

test/e2e/test/stacktraces.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ it('resolves/rejects', async () => {
292292
18| })
293293
19|
294294
295+
Caused by: 3
295296
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/4]⎯
296297
297298
"
@@ -371,6 +372,7 @@ it('resolves/rejects', async () => {
371372
18| })
372373
19|
373374
375+
Caused by: 3
374376
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/4]⎯
375377
376378
"

0 commit comments

Comments
 (0)