Skip to content

Commit

Permalink
fix(vitest): add correct location and snapshot fields in json reporte…
Browse files Browse the repository at this point in the history
…r, remove empty suite
  • Loading branch information
sheremet-va committed Mar 26, 2024
1 parent fee7d8b commit 3835880
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions packages/vitest/src/node/reporters/json.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { existsSync, promises as fs } from 'node:fs'
import { dirname, resolve } from 'pathe'
import type { Vitest } from '../../node'
import type { File, Reporter, Suite, Task, TaskState } from '../../types'
import type { File, Reporter, SnapshotSummary, Suite, TaskState } from '../../types'
import { getSuites, getTests } from '../../utils'
import { getOutputFile } from '../../utils/config-helpers'
import { parseErrorStacktrace } from '../../utils/source-map'

// for compatibility reasons, the reporter produces a JSON similar to the one produced by the Jest JSON reporter
// the following types are extracted from the Jest repository (and simplified)
// the commented-out fields are the missing ones

type Status = 'passed' | 'failed' | 'skipped' | 'pending' | 'todo' | 'disabled'
type Milliseconds = number
interface Callsite { line: number; column: number }
interface Callsite {
line: number
column: number
}

const StatusMap: Record<TaskState, Status> = {
fail: 'failed',
only: 'pending',
Expand All @@ -28,7 +31,7 @@ export interface JsonAssertionResult {
status: Status
title: string
duration?: Milliseconds | null
failureMessages: Array<string>
failureMessages: Array<string> | null
location?: Callsite | null
}

Expand Down Expand Up @@ -56,9 +59,9 @@ export interface JsonTestResults {
startTime: number
success: boolean
testResults: Array<JsonTestResult>
snapshot: SnapshotSummary
// coverageMap?: CoverageMap | null | undefined
// numRuntimeErrorTestSuites: number
// snapshot: SnapshotSummary
// wasInterrupted: boolean
}

Expand Down Expand Up @@ -103,25 +106,28 @@ export class JsonReporter implements Reporter {
startTime = this.start

const endTime = tests.reduce((prev, next) => Math.max(prev, (next.result?.startTime ?? 0) + (next.result?.duration ?? 0)), startTime)
const assertionResults = await Promise.all(tests.map(async (t) => {
const ancestorTitles = [] as string[]
const assertionResults = tests.map((t) => {
const ancestorTitles: string[] = []
let iter: Suite | undefined = t.suite
while (iter) {
// the root suite should not be reported
if (iter.id === '')
break
ancestorTitles.push(iter.name)
iter = iter.suite
}
ancestorTitles.reverse()

return {
ancestorTitles,
fullName: ancestorTitles.length > 0 ? `${ancestorTitles.join(' ')} ${t.name}` : t.name,
fullName: t.name ? [...ancestorTitles, t.name].join(' ') : ancestorTitles.join(' '),
status: StatusMap[t.result?.state || t.mode] || 'skipped',
title: t.name,
duration: t.result?.duration,
failureMessages: t.result?.errors?.map(e => e.message) || [],
location: await this.getFailureLocation(t),
} as JsonAssertionResult
}))
failureMessages: t.result?.errors?.map(e => e.stack || e.message) || [],
location: t.location || null,
} satisfies JsonAssertionResult
})

if (tests.some(t => t.result?.state === 'run')) {
this.ctx.logger.warn('WARNING: Some tests are still running when generating the JSON report.'
Expand Down Expand Up @@ -153,6 +159,7 @@ export class JsonReporter implements Reporter {
numFailedTests,
numPendingTests,
numTodoTests,
snapshot: this.ctx.snapshot.summary,
startTime: this.start,
success,
testResults,
Expand Down Expand Up @@ -187,21 +194,4 @@ export class JsonReporter implements Reporter {
this.ctx.logger.log(report)
}
}

protected async getFailureLocation(test: Task): Promise<Callsite | undefined> {
const error = test.result?.errors?.[0]
if (!error)
return

const project = this.ctx.getProjectByTaskId(test.id)
const stack = parseErrorStacktrace(error, {
getSourceMap: file => project.getBrowserSourceMapModuleById(file),
frameFilter: this.ctx.config.onStackTrace,
})
const frame = stack[0]
if (!frame)
return

return { line: frame.line, column: frame.column }
}
}

0 comments on commit 3835880

Please sign in to comment.