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(reporter): correct error source position in json report #1909

Merged
merged 14 commits into from Aug 29, 2022
11 changes: 6 additions & 5 deletions packages/vitest/src/node/reporters/json.ts
Expand Up @@ -4,7 +4,7 @@ import type { Vitest } from '../../node'
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'
import { interpretSourcePos, 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)
Expand Down Expand Up @@ -94,7 +94,7 @@ 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 = tests.map((t) => {
const assertionResults = await Promise.all(tests.map(async (t) => {
const ancestorTitles = [] as string[]
let iter: Suite | undefined = t.suite
while (iter) {
Expand All @@ -110,9 +110,9 @@ 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),
location: await this.getFailureLocation(t),
} as FormattedAssertionResult
})
}))

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 @@ -180,12 +180,13 @@ export class JsonReporter implements Reporter {
}
}

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

const stack = parseStacktrace(error)
await interpretSourcePos(stack, this.ctx)
const frame = stack[stack.length - 1]
if (!frame)
return
Expand Down
10 changes: 10 additions & 0 deletions test/reporters/src/context.ts
@@ -1,3 +1,4 @@
import type { ModuleGraph, ViteDevServer } from 'vite'
import type { Logger } from '../../../packages/vitest/src/node/logger'
import type { Vitest } from '../../../packages/vitest/src/node'
import type { StateManager } from '../../../packages/vitest/src/node/state'
Expand All @@ -15,13 +16,22 @@ export function getContext(): Context {
root: '/',
}

const moduleGraph: Partial<ModuleGraph> = {
getModuleById: () => undefined,
}

const server: Partial<ViteDevServer> = {
moduleGraph: moduleGraph as ModuleGraph,
}

const state: Partial<StateManager> = {
filesMap: new Map<string, File>(),
}

const context: Partial<Vitest> = {
state: state as StateManager,
config: config as ResolvedConfig,
server: server as ViteDevServer,
}

context.logger = {
Expand Down
5 changes: 5 additions & 0 deletions test/reporters/src/data.ts
Expand Up @@ -176,5 +176,10 @@ suite.tasks = tasks

const files = [file]

export function clearSourcePos() {
if (error.stacks)
error.stacks = undefined
}

export { files }

245 changes: 245 additions & 0 deletions test/reporters/tests/__snapshots__/json.test.ts.snap
@@ -0,0 +1,245 @@
// Vitest Snapshot v1

exports[`with comment 1`] = `
{
"numFailedTestSuites": 0,
"numFailedTests": 1,
"numPassedTestSuites": 3,
"numPassedTests": 8,
"numPendingTestSuites": 0,
"numPendingTests": 0,
"numTodoTests": 1,
"numTotalTestSuites": 3,
"numTotalTests": 9,
"startTime": 1742587001759,
"success": false,
"testResults": [
{
"assertionResults": [
{
"ancestorTitles": [
"suite",
"inner suite",
],
"duration": 1.4422860145568848,
"failureMessages": [
"expected 2.23606797749979 to equal 2",
],
"fullName": "suite inner suite Math.sqrt()",
"location": {
"column": 9,
"line": 10,
},
"status": "failed",
"title": "Math.sqrt()",
},
{
"ancestorTitles": [
"suite",
],
"duration": 1.0237109661102295,
"failureMessages": [],
"fullName": "suite JSON",
"status": "passed",
"title": "JSON",
},
{
"ancestorTitles": [
"suite",
],
"failureMessages": [],
"fullName": "suite async with timeout",
"status": "skipped",
"title": "async with timeout",
},
{
"ancestorTitles": [
"suite",
],
"duration": 100.50598406791687,
"failureMessages": [],
"fullName": "suite timeout",
"status": "passed",
"title": "timeout",
},
{
"ancestorTitles": [
"suite",
],
"duration": 20.184875011444092,
"failureMessages": [],
"fullName": "suite callback setup success ",
"status": "passed",
"title": "callback setup success ",
},
{
"ancestorTitles": [
"suite",
],
"duration": 0.33245420455932617,
"failureMessages": [],
"fullName": "suite callback test success ",
"status": "passed",
"title": "callback test success ",
},
{
"ancestorTitles": [
"suite",
],
"duration": 19.738605976104736,
"failureMessages": [],
"fullName": "suite callback setup success done(false)",
"status": "passed",
"title": "callback setup success done(false)",
},
{
"ancestorTitles": [
"suite",
],
"duration": 0.1923508644104004,
"failureMessages": [],
"fullName": "suite callback test success done(false)",
"status": "passed",
"title": "callback test success done(false)",
},
{
"ancestorTitles": [
"suite",
],
"failureMessages": [],
"fullName": "suite todo test",
"status": "todo",
"title": "todo test",
},
],
"endTime": 1742587001759,
"message": "",
"name": "/vitest/test/core/test/basic.test.ts",
"startTime": 1742587001759,
"status": "failed",
},
],
}
`;

exports[`without comment 1`] = `
{
"numFailedTestSuites": 0,
"numFailedTests": 1,
"numPassedTestSuites": 3,
"numPassedTests": 8,
"numPendingTestSuites": 0,
"numPendingTests": 0,
"numTodoTests": 1,
"numTotalTestSuites": 3,
"numTotalTests": 9,
"startTime": 1642587001759,
"success": false,
"testResults": [
{
"assertionResults": [
{
"ancestorTitles": [
"suite",
"inner suite",
],
"duration": 1.4422860145568848,
"failureMessages": [
"expected 2.23606797749979 to equal 2",
],
"fullName": "suite inner suite Math.sqrt()",
"location": {
"column": 9,
"line": 8,
},
"status": "failed",
"title": "Math.sqrt()",
},
{
"ancestorTitles": [
"suite",
],
"duration": 1.0237109661102295,
"failureMessages": [],
"fullName": "suite JSON",
"status": "passed",
"title": "JSON",
},
{
"ancestorTitles": [
"suite",
],
"failureMessages": [],
"fullName": "suite async with timeout",
"status": "skipped",
"title": "async with timeout",
},
{
"ancestorTitles": [
"suite",
],
"duration": 100.50598406791687,
"failureMessages": [],
"fullName": "suite timeout",
"status": "passed",
"title": "timeout",
},
{
"ancestorTitles": [
"suite",
],
"duration": 20.184875011444092,
"failureMessages": [],
"fullName": "suite callback setup success ",
"status": "passed",
"title": "callback setup success ",
},
{
"ancestorTitles": [
"suite",
],
"duration": 0.33245420455932617,
"failureMessages": [],
"fullName": "suite callback test success ",
"status": "passed",
"title": "callback test success ",
},
{
"ancestorTitles": [
"suite",
],
"duration": 19.738605976104736,
"failureMessages": [],
"fullName": "suite callback setup success done(false)",
"status": "passed",
"title": "callback setup success done(false)",
},
{
"ancestorTitles": [
"suite",
],
"duration": 0.1923508644104004,
"failureMessages": [],
"fullName": "suite callback test success done(false)",
"status": "passed",
"title": "callback test success done(false)",
},
{
"ancestorTitles": [
"suite",
],
"failureMessages": [],
"fullName": "suite todo test",
"status": "todo",
"title": "todo test",
},
],
"endTime": 1642587001759,
"message": "",
"name": "/vitest/test/core/test/basic.test.ts",
"startTime": 1642587001759,
"status": "failed",
},
],
}
`;