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: don't throw on null error, hide JSON results warning #1935

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/vitest/src/integrations/vi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { FakeTimerInstallOpts } from '@sinonjs/fake-timers'
import { parseStacktrace } from '../utils/source-map'
import type { VitestMocker } from '../runtime/mocker'
import { getWorkerState, resetModules, setTimeout } from '../utils'
import { createErrorWithTraceLimit } from '../utils/base'
import { FakeTimers } from './mock/timers'
import type { EnhancedSpy, MaybeMocked, MaybeMockedDeep, MaybePartiallyMocked, MaybePartiallyMockedDeep } from './spy'
import { fn, isMockFunction, spies, spyOn } from './spy'
Expand Down Expand Up @@ -108,7 +109,7 @@ class VitestUtils {
fn = fn

private getImporter() {
const err = new Error('mock')
const err = createErrorWithTraceLimit('mock', 3)
const [,, importer] = parseStacktrace(err, true)
return importer.file
}
Expand Down
9 changes: 6 additions & 3 deletions packages/vitest/src/node/cache/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ export class ResultsCache {

if (fs.existsSync(this.cachePath)) {
const resultsCache = await fs.promises.readFile(this.cachePath, 'utf8')
const { results, version } = JSON.parse(resultsCache)
this.cache = new Map(results)
this.version = version
try {
const { results, version } = JSON.parse(resultsCache)
this.cache = new Map(results)
this.version = version
}
catch {}
}
}

Expand Down
7 changes: 1 addition & 6 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,7 @@ export class Vitest {
this.runningPromise = undefined

this.cache.results.setConfig(resolved.root, resolved.cache)
try {
await this.cache.results.readFromCache()
}
catch (err) {
this.logger.error(`[vitest] Error, while trying to parse cache in ${this.cache.results.getCachePath()}:`, err)
}
await this.cache.results.readFromCache()
}

async initCoverageProvider() {
Expand Down
11 changes: 9 additions & 2 deletions packages/vitest/src/node/state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ErrorWithDiff, File, Task, TaskResultPack, UserConsoleLog } from '../types'
// can't import actual functions from utils, because it's incompatible with @vitest/browsers
import type { AggregateError as AggregateErrorPonyfill } from '../utils'
import { createErrorWithTraceLimit } from '../utils/base'

interface CollectingPromise {
promise: Promise<void>
Expand All @@ -24,10 +25,16 @@ export class StateManager {
errorsSet = new Set<unknown>()

catchError(err: unknown, type: string): void {
if (err == null)
err = createErrorWithTraceLimit('Unknown "null" error thrown')

if (isAggregateError(err))
return err.errors.forEach(error => this.catchError(error, type));
return err.errors.forEach(error => this.catchError(error, type))

if (typeof err !== 'object')
err = new Error(String(err))

(err as ErrorWithDiff).type = type
;(err as ErrorWithDiff).type = type
this.errorsSet.add(err)
}

Expand Down
18 changes: 18 additions & 0 deletions packages/vitest/src/utils/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,21 @@ export function shuffle<T>(array: T[], seed = RealDate.now()): T[] {

return array
}

// removes stack trace from error, when we don't really need it
// by default stack trace limit is 100 in vitest, but we might
// create errors just to get the last line of stack trace behind the scenes
export function createErrorWithTraceLimit(message?: string, limit = 0) {
if (!('stackTraceLimit' in Error)) {
const err = new Error(message)
if (limit === 0)
err.stack = ''
return err
}

const oldLimit = Error.stackTraceLimit
Error.stackTraceLimit = limit
const err = new Error(message)
Error.stackTraceLimit = oldLimit
return err
}