Skip to content

Commit 6efe61a

Browse files
authoredMar 24, 2023
fix: do not fail, when primitive error is thrown (#3074)
1 parent 908edc2 commit 6efe61a

File tree

7 files changed

+23
-9
lines changed

7 files changed

+23
-9
lines changed
 

‎packages/vitest/src/api/setup.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { ViteDevServer } from 'vite'
99
import { API_PATH } from '../constants'
1010
import type { Vitest } from '../node'
1111
import type { File, ModuleGraphData, Reporter, TaskResultPack, UserConsoleLog } from '../types'
12-
import { getModuleGraph } from '../utils'
12+
import { getModuleGraph, isPrimitive } from '../utils'
1313
import { parseErrorStacktrace } from '../utils/source-map'
1414
import type { TransformResultWithSource, WebSocketEvents, WebSocketHandlers } from './types'
1515

@@ -140,10 +140,11 @@ class WebSocketReporter implements Reporter {
140140

141141
packs.forEach(([, result]) => {
142142
// TODO remove after "error" deprecation is removed
143-
if (result?.error)
143+
if (result?.error && !isPrimitive(result.error))
144144
result.error.stacks = parseErrorStacktrace(result.error)
145145
result?.errors?.forEach((error) => {
146-
error.stacks = parseErrorStacktrace(error)
146+
if (!isPrimitive(error))
147+
error.stacks = parseErrorStacktrace(error)
147148
})
148149
})
149150

‎packages/vitest/src/node/core.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ export class Vitest {
721721
isBrowserEnabled() {
722722
if (this.config.browser.enabled)
723723
return true
724-
return (this.config.poolMatchGlobs || []).some(([, pool]) => pool === 'browser')
724+
return this.config.poolMatchGlobs?.length && this.config.poolMatchGlobs.some(([, pool]) => pool === 'browser')
725725
}
726726

727727
// The server needs to be running for communication

‎packages/vitest/src/node/error.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { ErrorWithDiff, ParsedStack } from '../types'
99
import { lineSplitRE, parseErrorStacktrace, positionToOffset } from '../utils/source-map'
1010
import { F_POINTER } from '../utils/figures'
1111
import { TypeCheckError } from '../typecheck/typechecker'
12+
import { isPrimitive } from '../utils'
1213
import type { Vitest } from './core'
1314
import { divider } from './reporters/renderers/utils'
1415
import type { Logger } from './logger'
@@ -23,10 +24,10 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
2324
const { showCodeFrame = true, fullStack = false, type } = options
2425
let e = error as ErrorWithDiff
2526

26-
if (typeof error === 'string') {
27+
if (isPrimitive(e)) {
2728
e = {
28-
message: error.split(/\n/g)[0],
29-
stack: error,
29+
message: String(error).split(/\n/g)[0],
30+
stack: String(error),
3031
} as any
3132
}
3233

‎packages/vitest/src/utils/base.ts

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export function groupBy<T, K extends string | number | symbol>(collection: T[],
1919
}, {} as Record<K, T[]>)
2020
}
2121

22+
export function isPrimitive(value: unknown) {
23+
return value === null || (typeof value !== 'function' && typeof value !== 'object')
24+
}
25+
2226
export function getAllMockableProperties(obj: any, isModule: boolean) {
2327
const allProps = new Map<string | symbol, { key: string | symbol; descriptor: PropertyDescriptor }>()
2428
let curr = obj

‎packages/vitest/src/utils/source-map.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { resolve } from 'pathe'
22
import type { ErrorWithDiff, ParsedStack } from '../types'
3-
import { notNullish } from './base'
3+
import { isPrimitive, notNullish } from './base'
44

55
export const lineSplitRE = /\r?\n/
66

@@ -92,7 +92,7 @@ export function parseStacktrace(stack: string, full = false): ParsedStack[] {
9292
}
9393

9494
export function parseErrorStacktrace(e: ErrorWithDiff, full = false): ParsedStack[] {
95-
if (!e)
95+
if (!e || isPrimitive(e))
9696
return []
9797

9898
if (e.stacks)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { test } from 'vitest'
2+
3+
test('primitive error thrown', () => {
4+
// eslint-disable-next-line no-throw-literal
5+
throw 42
6+
})

‎test/fails/test/__snapshots__/runner.test.ts.snap

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ exports[`should fails > mock-import-proxy-module.test.ts > mock-import-proxy-mod
1919
2020
exports[`should fails > nested-suite.test.ts > nested-suite.test.ts 1`] = `"AssertionError: expected true to be false // Object.is equality"`;
2121
22+
exports[`should fails > primitive-error.test.ts > primitive-error.test.ts 1`] = `"Unknown Error: 42"`;
23+
2224
exports[`should fails > stall.test.ts > stall.test.ts 1`] = `
2325
"TypeError: failure
2426
TypeError: failure

0 commit comments

Comments
 (0)
Please sign in to comment.