Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not fail, when primitive error is thrown #3074

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/vitest/src/api/setup.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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