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

feat: add location to json reporter #1656

Merged
merged 5 commits into from
Jul 19, 2022
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
21 changes: 19 additions & 2 deletions packages/vitest/src/node/reporters/json.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { existsSync, promises as fs } from 'fs'
import { dirname, resolve } from 'pathe'
import type { Vitest } from '../../node'
import type { File, Reporter, Suite, TaskState } from '../../types'
import type { File, Reporter, Suite, TaskState, Test } from '../../types'
import { getSuites, getTests } from '../../utils'
import { getOutputFile } from '../../utils/config-helpers'
import { parseStacktrace } 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 }
const StatusMap: Record<TaskState, Status> = {
fail: 'failed',
only: 'pending',
Expand All @@ -27,7 +29,7 @@ interface FormattedAssertionResult {
title: string
duration?: Milliseconds | null
failureMessages: Array<string>
// location?: Callsite | null
location?: Callsite | null
}

interface FormattedTestResult {
Expand Down Expand Up @@ -108,6 +110,7 @@ export class JsonReporter implements Reporter {
title: t.name,
duration: t.result?.duration,
failureMessages: t.result?.error?.message == null ? [] : [t.result.error.message],
location: this.getFailureLocation(t),
} as FormattedAssertionResult
})

Expand Down Expand Up @@ -176,4 +179,18 @@ export class JsonReporter implements Reporter {
this.ctx.logger.log(report)
}
}

protected getFailureLocation(test: Test): Callsite | undefined {
const error = test.result?.error
if (!error)
return

const stack = parseStacktrace(error)
const frame = stack[stack.length - 1]
if (!frame)
return

const pos = frame.sourcePos || frame
return { line: pos.line, column: pos.column }
}
}
32 changes: 28 additions & 4 deletions test/reporters/tests/__snapshots__/reporters.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ exports[`json reporter (no outputFile entry) 1`] = `
"expected 2.23606797749979 to equal 2",
],
"fullName": "suite inner suite Math.sqrt()",
"location": {
"column": 32,
"line": 8,
},
"status": "failed",
"title": "Math.sqrt()",
},
Expand Down Expand Up @@ -380,6 +384,10 @@ exports[`json reporter 1`] = `
"expected 2.23606797749979 to equal 2",
],
"fullName": "suite inner suite Math.sqrt()",
"location": {
"column": 32,
"line": 8,
},
"status": "failed",
"title": "Math.sqrt()",
},
Expand Down Expand Up @@ -504,7 +512,11 @@ exports[`json reporter with outputFile 2`] = `
\\"duration\\": 1.4422860145568848,
\\"failureMessages\\": [
\\"expected 2.23606797749979 to equal 2\\"
]
],
\\"location\\": {
\\"line\\": 8,
\\"column\\": 32
}
},
{
\\"ancestorTitles\\": [
Expand Down Expand Up @@ -627,7 +639,11 @@ exports[`json reporter with outputFile in non-existing directory 2`] = `
\\"duration\\": 1.4422860145568848,
\\"failureMessages\\": [
\\"expected 2.23606797749979 to equal 2\\"
]
],
\\"location\\": {
\\"line\\": 8,
\\"column\\": 32
}
},
{
\\"ancestorTitles\\": [
Expand Down Expand Up @@ -750,7 +766,11 @@ exports[`json reporter with outputFile object 2`] = `
\\"duration\\": 1.4422860145568848,
\\"failureMessages\\": [
\\"expected 2.23606797749979 to equal 2\\"
]
],
\\"location\\": {
\\"line\\": 8,
\\"column\\": 32
}
},
{
\\"ancestorTitles\\": [
Expand Down Expand Up @@ -873,7 +893,11 @@ exports[`json reporter with outputFile object in non-existing directory 2`] = `
\\"duration\\": 1.4422860145568848,
\\"failureMessages\\": [
\\"expected 2.23606797749979 to equal 2\\"
]
],
\\"location\\": {
\\"line\\": 8,
\\"column\\": 32
}
},
{
\\"ancestorTitles\\": [
Expand Down