diff --git a/packages/vitest/src/node/reporters/json.ts b/packages/vitest/src/node/reporters/json.ts index bdad4fe9a3f7..8d102e7ee4cb 100644 --- a/packages/vitest/src/node/reporters/json.ts +++ b/packages/vitest/src/node/reporters/json.ts @@ -6,13 +6,20 @@ import { getSuites, getTests } from '../../utils' // 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) +type Milliseconds = number interface TestResult { displayName?: string failureMessage?: string | null skipped: boolean status?: string testFilePath?: string + perfStats: { + end?: Milliseconds + runtime?: Milliseconds + start?: Milliseconds + } } + interface AggregatedResult { numFailedTests: number numFailedTestSuites: number @@ -54,6 +61,11 @@ export class JsonReporter implements Reporter { const success = numFailedTestSuites === 0 && numFailedTests === 0 const testResults: Array = tests.map(t => ({ + perfStats: { + runtime: t.result?.duration, + start: t.result?.startTime, + end: t.result?.duration && t.result?.startTime && t.result.duration + t.result.startTime, + }, displayName: t.name, failureMessage: t.result?.error?.message, skipped: t.mode === 'skip', diff --git a/packages/vitest/src/runtime/run.ts b/packages/vitest/src/runtime/run.ts index cdf48bf2aea2..4726f6508e76 100644 --- a/packages/vitest/src/runtime/run.ts +++ b/packages/vitest/src/runtime/run.ts @@ -71,6 +71,7 @@ export async function runTest(test: Test) { test.result = { state: 'run', + startTime: Date.now(), } updateTask(test) @@ -154,6 +155,7 @@ export async function runSuite(suite: Suite) { suite.result = { state: 'run', + startTime: Date.now(), } updateTask(suite) diff --git a/packages/vitest/src/types/tasks.ts b/packages/vitest/src/types/tasks.ts index 22143492f0cb..fb387764bac7 100644 --- a/packages/vitest/src/types/tasks.ts +++ b/packages/vitest/src/types/tasks.ts @@ -18,6 +18,7 @@ export interface TaskBase { export interface TaskResult { state: TaskState duration?: number + startTime?: number error?: ErrorWithDiff hooks?: Partial> } diff --git a/test/reporters/tests/__snapshots__/reporters.spec.ts.snap b/test/reporters/tests/__snapshots__/reporters.spec.ts.snap index daacc0152ac2..6aeae86d21bb 100644 --- a/test/reporters/tests/__snapshots__/reporters.spec.ts.snap +++ b/test/reporters/tests/__snapshots__/reporters.spec.ts.snap @@ -31,70 +31,91 @@ AssertionError: expected 2.23606797749979 to equal 2 `; exports[`json reporter 1`] = ` -"{ - \\"numTotalTestSuites\\": 3, - \\"numPassedTestSuites\\": 3, - \\"numFailedTestSuites\\": 0, - \\"numPendingTestSuites\\": 0, - \\"numTotalTests\\": 8, - \\"numPassedTests\\": 7, - \\"numFailedTests\\": 1, - \\"numPendingTests\\": 0, - \\"numTodoTests\\": 0, - \\"startTime\\": 1642587001759, - \\"success\\": false, - \\"testResults\\": [ +{ + "numFailedTestSuites": 0, + "numFailedTests": 1, + "numPassedTestSuites": 3, + "numPassedTests": 7, + "numPendingTestSuites": 0, + "numPendingTests": 0, + "numTodoTests": 0, + "numTotalTestSuites": 3, + "numTotalTests": 8, + "startTime": 1642587001759, + "success": false, + "testResults": [ { - \\"displayName\\": \\"Math.sqrt()\\", - \\"failureMessage\\": \\"expected 2.23606797749979 to equal 2\\", - \\"skipped\\": false, - \\"status\\": \\"fail\\", - \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" + "displayName": "Math.sqrt()", + "failureMessage": "expected 2.23606797749979 to equal 2", + "perfStats": { + "runtime": 1.4422860145568848, + }, + "skipped": false, + "status": "fail", + "testFilePath": "/vitest/test/core/test/basic.test.ts", }, { - \\"displayName\\": \\"JSON\\", - \\"skipped\\": false, - \\"status\\": \\"pass\\", - \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" + "displayName": "JSON", + "perfStats": { + "runtime": 1.0237109661102295, + }, + "skipped": false, + "status": "pass", + "testFilePath": "/vitest/test/core/test/basic.test.ts", }, { - \\"displayName\\": \\"async with timeout\\", - \\"skipped\\": true, - \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" + "displayName": "async with timeout", + "perfStats": {}, + "skipped": true, + "testFilePath": "/vitest/test/core/test/basic.test.ts", }, { - \\"displayName\\": \\"timeout\\", - \\"skipped\\": false, - \\"status\\": \\"pass\\", - \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" + "displayName": "timeout", + "perfStats": { + "runtime": 100.50598406791687, + }, + "skipped": false, + "status": "pass", + "testFilePath": "/vitest/test/core/test/basic.test.ts", }, { - \\"displayName\\": \\"callback setup success \\", - \\"skipped\\": false, - \\"status\\": \\"pass\\", - \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" + "displayName": "callback setup success ", + "perfStats": { + "runtime": 20.184875011444092, + }, + "skipped": false, + "status": "pass", + "testFilePath": "/vitest/test/core/test/basic.test.ts", }, { - \\"displayName\\": \\"callback test success \\", - \\"skipped\\": false, - \\"status\\": \\"pass\\", - \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" + "displayName": "callback test success ", + "perfStats": { + "runtime": 0.33245420455932617, + }, + "skipped": false, + "status": "pass", + "testFilePath": "/vitest/test/core/test/basic.test.ts", }, { - \\"displayName\\": \\"callback setup success done(false)\\", - \\"skipped\\": false, - \\"status\\": \\"pass\\", - \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" + "displayName": "callback setup success done(false)", + "perfStats": { + "runtime": 19.738605976104736, + }, + "skipped": false, + "status": "pass", + "testFilePath": "/vitest/test/core/test/basic.test.ts", }, { - \\"displayName\\": \\"callback test success done(false)\\", - \\"skipped\\": false, - \\"status\\": \\"pass\\", - \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" - } - ] + "displayName": "callback test success done(false)", + "perfStats": { + "runtime": 0.1923508644104004, + }, + "skipped": false, + "status": "pass", + "testFilePath": "/vitest/test/core/test/basic.test.ts", + }, + ], } -" `; exports[`tap reporter 1`] = ` diff --git a/test/reporters/tests/custom-reporter.spec.ts b/test/reporters/tests/custom-reporter.spec.ts index 680affdc16bd..f63075ce955b 100644 --- a/test/reporters/tests/custom-reporter.spec.ts +++ b/test/reporters/tests/custom-reporter.spec.ts @@ -16,4 +16,4 @@ test('custom reporters work', async() => { }) expect(stdout).toContain('hello from custom reporter') -}, 20000) +}, 40000) diff --git a/test/reporters/tests/reporters.spec.ts b/test/reporters/tests/reporters.spec.ts index 7e41d83cb463..5b341fd5e632 100644 --- a/test/reporters/tests/reporters.spec.ts +++ b/test/reporters/tests/reporters.spec.ts @@ -67,5 +67,5 @@ test('json reporter', async() => { await reporter.onFinished(files) // Assert - expect(context.output).toMatchSnapshot() + expect(JSON.parse(context.output)).toMatchSnapshot() })