Skip to content

Commit

Permalink
fix!: add correct location and snapshot fields in json reporter (vite…
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored and AriPerkkio committed May 7, 2024
1 parent 3199494 commit a2fd263
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 50 deletions.
41 changes: 14 additions & 27 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 @@ -104,7 +107,7 @@ export class JsonReporter implements Reporter {

const endTime = tests.reduce((prev, next) => Math.max(prev, (next.result?.startTime ?? 0) + (next.result?.duration ?? 0)), startTime)
const assertionResults = tests.map((t) => {
const ancestorTitles = [] as string[]
const ancestorTitles: string[] = []
let iter: Suite | undefined = t.suite
while (iter) {
ancestorTitles.push(iter.name)
Expand All @@ -114,13 +117,13 @@ export class JsonReporter implements Reporter {

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: this.getFailureLocation(t),
} as JsonAssertionResult
failureMessages: t.result?.errors?.map(e => e.stack || e.message) || [],
location: t.location,
} satisfies JsonAssertionResult
})

if (tests.some(t => t.result?.state === 'run')) {
Expand Down Expand Up @@ -153,6 +156,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 +191,4 @@ export class JsonReporter implements Reporter {
this.ctx.logger.log(report)
}
}

protected getFailureLocation(test: Task): 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 }
}
}
1 change: 0 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/reporters/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export function getContext(): Context {
config: config as ResolvedConfig,
server: server as ViteDevServer,
getProjectByTaskId: () => ({ getBrowserSourceMapModuleById: () => undefined }) as any,
snapshot: {
summary: { added: 100, _test: true },
} as any,
}

context.logger = {
Expand Down
24 changes: 14 additions & 10 deletions test/reporters/src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const file: File = {
file.file = file

const suite: Suite = {
id: '',
id: '1223128da3_0',
type: 'suite',
name: 'suite',
mode: 'run',
Expand Down Expand Up @@ -47,7 +47,7 @@ error.stack = 'AssertionError: expected 2.23606797749979 to equal 2\n'

const tasks: Task[] = [
{
id: '1223128da3_0',
id: '1223128da3_0_0',
type: 'test',
name: 'Math.sqrt()',
mode: 'run',
Expand All @@ -60,10 +60,14 @@ const tasks: Task[] = [
errors: [error],
duration: 1.4422860145568848,
},
location: {
column: 32,
line: 8,
},
context: null as any,
},
{
id: '1223128da3_1',
id: '1223128da3_0_1',
type: 'test',
name: 'JSON',
mode: 'run',
Expand All @@ -75,7 +79,7 @@ const tasks: Task[] = [
context: null as any,
},
{
id: '1223128da3_3',
id: '1223128da3_0_3',
type: 'test',
name: 'async with timeout',
mode: 'skip',
Expand All @@ -87,7 +91,7 @@ const tasks: Task[] = [
context: null as any,
},
{
id: '1223128da3_4',
id: '1223128da3_0_4',
type: 'test',
name: 'timeout',
mode: 'run',
Expand All @@ -99,7 +103,7 @@ const tasks: Task[] = [
context: null as any,
},
{
id: '1223128da3_5',
id: '1223128da3_0_5',
type: 'test',
name: 'callback setup success ',
mode: 'run',
Expand All @@ -111,7 +115,7 @@ const tasks: Task[] = [
context: null as any,
},
{
id: '1223128da3_6',
id: '1223128da3_0_6',
type: 'test',
name: 'callback test success ',
mode: 'run',
Expand All @@ -123,7 +127,7 @@ const tasks: Task[] = [
context: null as any,
},
{
id: '1223128da3_7',
id: '1223128da3_0_7',
type: 'test',
name: 'callback setup success done(false)',
mode: 'run',
Expand All @@ -135,7 +139,7 @@ const tasks: Task[] = [
context: null as any,
},
{
id: '1223128da3_8',
id: '1223128da3_0_8',
type: 'test',
name: 'callback test success done(false)',
mode: 'run',
Expand All @@ -155,7 +159,7 @@ const tasks: Task[] = [
],
},
{
id: '1223128da3_9',
id: '1223128da3_0_9',
type: 'test',
name: 'todo test',
mode: 'todo',
Expand Down
7 changes: 4 additions & 3 deletions test/reporters/tests/__snapshots__/json.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ exports[`json reporter > generates correct report 1`] = `
{
"ancestorTitles": [],
"failureMessages": [
"expected 2 to deeply equal 1",
"AssertionError: expected 2 to deeply equal 1
at <root>/test/reporters/fixtures/json-fail.test.ts:8:13",
],
"fullName": "should fail",
"location": {
"column": 13,
"line": 8,
"column": 1,
"line": 5,
},
"status": "failed",
"title": "should fail",
Expand Down
Loading

0 comments on commit a2fd263

Please sign in to comment.