From 4b2def411328f561dbe14248b5eb42277e42ed60 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Sun, 13 Mar 2022 15:53:30 +0800 Subject: [PATCH 1/6] feat: add perf stats to json reporter --- packages/vitest/src/node/reporters/json.ts | 12 ++++++++++++ packages/vitest/src/runtime/run.ts | 4 ++++ packages/vitest/src/types/tasks.ts | 1 + 3 files changed, 17 insertions(+) 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..4299b72042bb 100644 --- a/packages/vitest/src/runtime/run.ts +++ b/packages/vitest/src/runtime/run.ts @@ -67,6 +67,7 @@ export async function runTest(test: Test) { return } + const startDate = Date.now() const start = performance.now() test.result = { @@ -127,6 +128,7 @@ export async function runTest(test: Test) { getSnapshotClient().clearTest() + test.result.startTime = startDate test.result.duration = performance.now() - start __vitest_worker__.current = undefined @@ -150,6 +152,7 @@ export async function runSuite(suite: Suite) { return } + const startDate = Date.now() const start = performance.now() suite.result = { @@ -185,6 +188,7 @@ export async function runSuite(suite: Suite) { } } suite.result.duration = performance.now() - start + suite.result.startTime = startDate if (suite.mode === 'run') { if (!hasTests(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> } From fc2a5948d0fbe38ba9b84544c50527165d688d26 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Sun, 13 Mar 2022 16:18:52 +0800 Subject: [PATCH 2/6] test: update snapshot --- .../__snapshots__/reporters.spec.ts.snap | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/reporters/tests/__snapshots__/reporters.spec.ts.snap b/test/reporters/tests/__snapshots__/reporters.spec.ts.snap index daacc0152ac2..e2c825ff6716 100644 --- a/test/reporters/tests/__snapshots__/reporters.spec.ts.snap +++ b/test/reporters/tests/__snapshots__/reporters.spec.ts.snap @@ -45,6 +45,9 @@ exports[`json reporter 1`] = ` \\"success\\": false, \\"testResults\\": [ { + \\"perfStats\\": { + \\"runtime\\": 1.4422860145568848 + }, \\"displayName\\": \\"Math.sqrt()\\", \\"failureMessage\\": \\"expected 2.23606797749979 to equal 2\\", \\"skipped\\": false, @@ -52,41 +55,60 @@ exports[`json reporter 1`] = ` \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" }, { + \\"perfStats\\": { + \\"runtime\\": 1.0237109661102295 + }, \\"displayName\\": \\"JSON\\", \\"skipped\\": false, \\"status\\": \\"pass\\", \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" }, { + \\"perfStats\\": {}, \\"displayName\\": \\"async with timeout\\", \\"skipped\\": true, \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" }, { + \\"perfStats\\": { + \\"runtime\\": 100.50598406791687 + }, \\"displayName\\": \\"timeout\\", \\"skipped\\": false, \\"status\\": \\"pass\\", \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" }, { + \\"perfStats\\": { + \\"runtime\\": 20.184875011444092 + }, \\"displayName\\": \\"callback setup success \\", \\"skipped\\": false, \\"status\\": \\"pass\\", \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" }, { + \\"perfStats\\": { + \\"runtime\\": 0.33245420455932617 + }, \\"displayName\\": \\"callback test success \\", \\"skipped\\": false, \\"status\\": \\"pass\\", \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" }, { + \\"perfStats\\": { + \\"runtime\\": 19.738605976104736 + }, \\"displayName\\": \\"callback setup success done(false)\\", \\"skipped\\": false, \\"status\\": \\"pass\\", \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" }, { + \\"perfStats\\": { + \\"runtime\\": 0.1923508644104004 + }, \\"displayName\\": \\"callback test success done(false)\\", \\"skipped\\": false, \\"status\\": \\"pass\\", From 8a2f803ac80266dde56e101b507376aa103430b4 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Sun, 13 Mar 2022 16:29:57 +0800 Subject: [PATCH 3/6] chore: trigger ci From b3f382ec7f6434ccb89beb53db1f2ba7a88e073d Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Sun, 13 Mar 2022 16:55:21 +0800 Subject: [PATCH 4/6] test: update json reporter snapshot --- .../__snapshots__/reporters.spec.ts.snap | 125 +++++++++--------- test/reporters/tests/reporters.spec.ts | 2 +- 2 files changed, 63 insertions(+), 64 deletions(-) diff --git a/test/reporters/tests/__snapshots__/reporters.spec.ts.snap b/test/reporters/tests/__snapshots__/reporters.spec.ts.snap index e2c825ff6716..6aeae86d21bb 100644 --- a/test/reporters/tests/__snapshots__/reporters.spec.ts.snap +++ b/test/reporters/tests/__snapshots__/reporters.spec.ts.snap @@ -31,92 +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": [ { - \\"perfStats\\": { - \\"runtime\\": 1.4422860145568848 + "displayName": "Math.sqrt()", + "failureMessage": "expected 2.23606797749979 to equal 2", + "perfStats": { + "runtime": 1.4422860145568848, }, - \\"displayName\\": \\"Math.sqrt()\\", - \\"failureMessage\\": \\"expected 2.23606797749979 to equal 2\\", - \\"skipped\\": false, - \\"status\\": \\"fail\\", - \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" + "skipped": false, + "status": "fail", + "testFilePath": "/vitest/test/core/test/basic.test.ts", }, { - \\"perfStats\\": { - \\"runtime\\": 1.0237109661102295 + "displayName": "JSON", + "perfStats": { + "runtime": 1.0237109661102295, }, - \\"displayName\\": \\"JSON\\", - \\"skipped\\": false, - \\"status\\": \\"pass\\", - \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" + "skipped": false, + "status": "pass", + "testFilePath": "/vitest/test/core/test/basic.test.ts", }, { - \\"perfStats\\": {}, - \\"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", }, { - \\"perfStats\\": { - \\"runtime\\": 100.50598406791687 + "displayName": "timeout", + "perfStats": { + "runtime": 100.50598406791687, }, - \\"displayName\\": \\"timeout\\", - \\"skipped\\": false, - \\"status\\": \\"pass\\", - \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" + "skipped": false, + "status": "pass", + "testFilePath": "/vitest/test/core/test/basic.test.ts", }, { - \\"perfStats\\": { - \\"runtime\\": 20.184875011444092 + "displayName": "callback setup success ", + "perfStats": { + "runtime": 20.184875011444092, }, - \\"displayName\\": \\"callback setup success \\", - \\"skipped\\": false, - \\"status\\": \\"pass\\", - \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" + "skipped": false, + "status": "pass", + "testFilePath": "/vitest/test/core/test/basic.test.ts", }, { - \\"perfStats\\": { - \\"runtime\\": 0.33245420455932617 + "displayName": "callback test success ", + "perfStats": { + "runtime": 0.33245420455932617, }, - \\"displayName\\": \\"callback test success \\", - \\"skipped\\": false, - \\"status\\": \\"pass\\", - \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" + "skipped": false, + "status": "pass", + "testFilePath": "/vitest/test/core/test/basic.test.ts", }, { - \\"perfStats\\": { - \\"runtime\\": 19.738605976104736 + "displayName": "callback setup success done(false)", + "perfStats": { + "runtime": 19.738605976104736, }, - \\"displayName\\": \\"callback setup success done(false)\\", - \\"skipped\\": false, - \\"status\\": \\"pass\\", - \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" + "skipped": false, + "status": "pass", + "testFilePath": "/vitest/test/core/test/basic.test.ts", }, { - \\"perfStats\\": { - \\"runtime\\": 0.1923508644104004 + "displayName": "callback test success done(false)", + "perfStats": { + "runtime": 0.1923508644104004, }, - \\"displayName\\": \\"callback test success done(false)\\", - \\"skipped\\": false, - \\"status\\": \\"pass\\", - \\"testFilePath\\": \\"/vitest/test/core/test/basic.test.ts\\" - } - ] + "skipped": false, + "status": "pass", + "testFilePath": "/vitest/test/core/test/basic.test.ts", + }, + ], } -" `; exports[`tap reporter 1`] = ` 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() }) From f848bb1dbbcb1956b29843855ac07916708f2867 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Sun, 13 Mar 2022 17:05:57 +0800 Subject: [PATCH 5/6] test: inc custom reporter test timeout --- test/reporters/tests/custom-reporter.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) From 7a45ae509aac42ee4957e37cc34b79f69bbd9b7f Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Sun, 13 Mar 2022 17:11:17 +0800 Subject: [PATCH 6/6] refactor: move expr --- packages/vitest/src/runtime/run.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/vitest/src/runtime/run.ts b/packages/vitest/src/runtime/run.ts index 4299b72042bb..4726f6508e76 100644 --- a/packages/vitest/src/runtime/run.ts +++ b/packages/vitest/src/runtime/run.ts @@ -67,11 +67,11 @@ export async function runTest(test: Test) { return } - const startDate = Date.now() const start = performance.now() test.result = { state: 'run', + startTime: Date.now(), } updateTask(test) @@ -128,7 +128,6 @@ export async function runTest(test: Test) { getSnapshotClient().clearTest() - test.result.startTime = startDate test.result.duration = performance.now() - start __vitest_worker__.current = undefined @@ -152,11 +151,11 @@ export async function runSuite(suite: Suite) { return } - const startDate = Date.now() const start = performance.now() suite.result = { state: 'run', + startTime: Date.now(), } updateTask(suite) @@ -188,7 +187,6 @@ export async function runSuite(suite: Suite) { } } suite.result.duration = performance.now() - start - suite.result.startTime = startDate if (suite.mode === 'run') { if (!hasTests(suite)) {