Skip to content

Commit

Permalink
fix: do not fail, when primitive error is thrown (#3074)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Mar 24, 2023
1 parent 908edc2 commit 6efe61a
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 9 deletions.
7 changes: 4 additions & 3 deletions packages/vitest/src/api/setup.ts
Expand Up @@ -9,7 +9,7 @@ import type { ViteDevServer } from 'vite'
import { API_PATH } from '../constants'
import type { Vitest } from '../node'
import type { File, ModuleGraphData, Reporter, TaskResultPack, UserConsoleLog } from '../types'
import { getModuleGraph } from '../utils'
import { getModuleGraph, isPrimitive } from '../utils'
import { parseErrorStacktrace } from '../utils/source-map'
import type { TransformResultWithSource, WebSocketEvents, WebSocketHandlers } from './types'

Expand Down Expand Up @@ -140,10 +140,11 @@ class WebSocketReporter implements Reporter {

packs.forEach(([, result]) => {
// TODO remove after "error" deprecation is removed
if (result?.error)
if (result?.error && !isPrimitive(result.error))
result.error.stacks = parseErrorStacktrace(result.error)
result?.errors?.forEach((error) => {
error.stacks = parseErrorStacktrace(error)
if (!isPrimitive(error))
error.stacks = parseErrorStacktrace(error)
})
})

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/core.ts
Expand Up @@ -721,7 +721,7 @@ export class Vitest {
isBrowserEnabled() {
if (this.config.browser.enabled)
return true
return (this.config.poolMatchGlobs || []).some(([, pool]) => pool === 'browser')
return this.config.poolMatchGlobs?.length && this.config.poolMatchGlobs.some(([, pool]) => pool === 'browser')
}

// The server needs to be running for communication
Expand Down
7 changes: 4 additions & 3 deletions packages/vitest/src/node/error.ts
Expand Up @@ -9,6 +9,7 @@ import type { ErrorWithDiff, ParsedStack } from '../types'
import { lineSplitRE, parseErrorStacktrace, positionToOffset } from '../utils/source-map'
import { F_POINTER } from '../utils/figures'
import { TypeCheckError } from '../typecheck/typechecker'
import { isPrimitive } from '../utils'
import type { Vitest } from './core'
import { divider } from './reporters/renderers/utils'
import type { Logger } from './logger'
Expand All @@ -23,10 +24,10 @@ export async function printError(error: unknown, ctx: Vitest, options: PrintErro
const { showCodeFrame = true, fullStack = false, type } = options
let e = error as ErrorWithDiff

if (typeof error === 'string') {
if (isPrimitive(e)) {
e = {
message: error.split(/\n/g)[0],
stack: error,
message: String(error).split(/\n/g)[0],
stack: String(error),
} as any
}

Expand Down
4 changes: 4 additions & 0 deletions packages/vitest/src/utils/base.ts
Expand Up @@ -19,6 +19,10 @@ export function groupBy<T, K extends string | number | symbol>(collection: T[],
}, {} as Record<K, T[]>)
}

export function isPrimitive(value: unknown) {
return value === null || (typeof value !== 'function' && typeof value !== 'object')
}

export function getAllMockableProperties(obj: any, isModule: boolean) {
const allProps = new Map<string | symbol, { key: string | symbol; descriptor: PropertyDescriptor }>()
let curr = obj
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/utils/source-map.ts
@@ -1,6 +1,6 @@
import { resolve } from 'pathe'
import type { ErrorWithDiff, ParsedStack } from '../types'
import { notNullish } from './base'
import { isPrimitive, notNullish } from './base'

export const lineSplitRE = /\r?\n/

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

export function parseErrorStacktrace(e: ErrorWithDiff, full = false): ParsedStack[] {
if (!e)
if (!e || isPrimitive(e))
return []

if (e.stacks)
Expand Down
6 changes: 6 additions & 0 deletions test/fails/fixtures/primitive-error.test.ts
@@ -0,0 +1,6 @@
import { test } from 'vitest'

test('primitive error thrown', () => {
// eslint-disable-next-line no-throw-literal
throw 42
})
2 changes: 2 additions & 0 deletions test/fails/test/__snapshots__/runner.test.ts.snap
Expand Up @@ -19,6 +19,8 @@ exports[`should fails > mock-import-proxy-module.test.ts > mock-import-proxy-mod
exports[`should fails > nested-suite.test.ts > nested-suite.test.ts 1`] = `"AssertionError: expected true to be false // Object.is equality"`;
exports[`should fails > primitive-error.test.ts > primitive-error.test.ts 1`] = `"Unknown Error: 42"`;
exports[`should fails > stall.test.ts > stall.test.ts 1`] = `
"TypeError: failure
TypeError: failure
Expand Down

0 comments on commit 6efe61a

Please sign in to comment.