From dd754c1032d38445a02a63c6e6ac7b7d34582b80 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Mon, 3 Jun 2024 09:01:12 +0200 Subject: [PATCH] feat!: add meta to json output (#5802) --- docs/advanced/metadata.md | 2 +- packages/vitest/src/node/reporters/json.ts | 4 +- .../tests/__snapshots__/json.test.ts.snap | 1 + .../__snapshots__/reporters.spec.ts.snap | 54 +++++++++++++++++++ test/reporters/tests/merge-reports.test.ts | 5 ++ 5 files changed, 64 insertions(+), 2 deletions(-) diff --git a/docs/advanced/metadata.md b/docs/advanced/metadata.md index 96982ea62129..2f493d3f0943 100644 --- a/docs/advanced/metadata.md +++ b/docs/advanced/metadata.md @@ -48,7 +48,7 @@ Vitest uses different methods to communicate with the Node.js process. - If Vitest uses child process, the data will be send as a serialized Buffer via [`process.send`](https://nodejs.org/api/process.html#processsendmessage-sendhandle-options-callback) API - If Vitest runs tests in the browser, the data will be stringified using [flatted](https://www.npmjs.com/package/flatted) package -The general rule of thumb is that you can send almost anything, except for functions, Promises, regexp (`v8.stringify` cannot serialize it, but you can send a string version and parse it in the Node.js process yourself), and other non-serializable data, but you can have cyclic references inside. +This property is also present on every test in the `json` reporter, so make sure that data can be serialized into JSON. Also, make sure you serialize [Error properties](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#error_types) before you set them. ::: diff --git a/packages/vitest/src/node/reporters/json.ts b/packages/vitest/src/node/reporters/json.ts index dc050c07fd2b..3bc9a168d113 100644 --- a/packages/vitest/src/node/reporters/json.ts +++ b/packages/vitest/src/node/reporters/json.ts @@ -1,7 +1,7 @@ import { existsSync, promises as fs } from 'node:fs' import { dirname, resolve } from 'pathe' import type { Vitest } from '../../node' -import type { File, Reporter, SnapshotSummary, Suite, TaskState } from '../../types' +import type { File, Reporter, SnapshotSummary, Suite, TaskMeta, TaskState } from '../../types' import { getSuites, getTests } from '../../utils' import { getOutputFile } from '../../utils/config-helpers' @@ -30,6 +30,7 @@ export interface JsonAssertionResult { fullName: string status: Status title: string + meta: TaskMeta duration?: Milliseconds | null failureMessages: Array | null location?: Callsite | null @@ -123,6 +124,7 @@ export class JsonReporter implements Reporter { duration: t.result?.duration, failureMessages: t.result?.errors?.map(e => e.stack || e.message) || [], location: t.location, + meta: t.meta, } satisfies JsonAssertionResult }) diff --git a/test/reporters/tests/__snapshots__/json.test.ts.snap b/test/reporters/tests/__snapshots__/json.test.ts.snap index 1db05f14ca18..23a5e116c801 100644 --- a/test/reporters/tests/__snapshots__/json.test.ts.snap +++ b/test/reporters/tests/__snapshots__/json.test.ts.snap @@ -12,6 +12,7 @@ exports[`json reporter > generates correct report 1`] = ` "column": 1, "line": 5, }, + "meta": {}, "status": "failed", "title": "should fail", } diff --git a/test/reporters/tests/__snapshots__/reporters.spec.ts.snap b/test/reporters/tests/__snapshots__/reporters.spec.ts.snap index 012c01f704be..088b1b17458f 100644 --- a/test/reporters/tests/__snapshots__/reporters.spec.ts.snap +++ b/test/reporters/tests/__snapshots__/reporters.spec.ts.snap @@ -105,6 +105,7 @@ exports[`json reporter (no outputFile entry) 1`] = ` "column": 32, "line": 8, }, + "meta": {}, "status": "failed", "title": "Math.sqrt()", }, @@ -115,6 +116,7 @@ exports[`json reporter (no outputFile entry) 1`] = ` "duration": 1.0237109661102295, "failureMessages": [], "fullName": "suite JSON", + "meta": {}, "status": "passed", "title": "JSON", }, @@ -124,6 +126,7 @@ exports[`json reporter (no outputFile entry) 1`] = ` ], "failureMessages": [], "fullName": "suite async with timeout", + "meta": {}, "status": "skipped", "title": "async with timeout", }, @@ -134,6 +137,7 @@ exports[`json reporter (no outputFile entry) 1`] = ` "duration": 100.50598406791687, "failureMessages": [], "fullName": "suite timeout", + "meta": {}, "status": "passed", "title": "timeout", }, @@ -144,6 +148,7 @@ exports[`json reporter (no outputFile entry) 1`] = ` "duration": 20.184875011444092, "failureMessages": [], "fullName": "suite callback setup success ", + "meta": {}, "status": "passed", "title": "callback setup success ", }, @@ -154,6 +159,7 @@ exports[`json reporter (no outputFile entry) 1`] = ` "duration": 0.33245420455932617, "failureMessages": [], "fullName": "suite callback test success ", + "meta": {}, "status": "passed", "title": "callback test success ", }, @@ -164,6 +170,7 @@ exports[`json reporter (no outputFile entry) 1`] = ` "duration": 19.738605976104736, "failureMessages": [], "fullName": "suite callback setup success done(false)", + "meta": {}, "status": "passed", "title": "callback setup success done(false)", }, @@ -174,6 +181,7 @@ exports[`json reporter (no outputFile entry) 1`] = ` "duration": 0.1923508644104004, "failureMessages": [], "fullName": "suite callback test success done(false)", + "meta": {}, "status": "passed", "title": "callback test success done(false)", }, @@ -183,6 +191,7 @@ exports[`json reporter (no outputFile entry) 1`] = ` ], "failureMessages": [], "fullName": "suite todo test", + "meta": {}, "status": "todo", "title": "todo test", }, @@ -240,6 +249,7 @@ exports[`json reporter 1`] = ` "column": 32, "line": 8, }, + "meta": {}, "status": "failed", "title": "Math.sqrt()", }, @@ -250,6 +260,7 @@ exports[`json reporter 1`] = ` "duration": 1.0237109661102295, "failureMessages": [], "fullName": "suite JSON", + "meta": {}, "status": "passed", "title": "JSON", }, @@ -259,6 +270,7 @@ exports[`json reporter 1`] = ` ], "failureMessages": [], "fullName": "suite async with timeout", + "meta": {}, "status": "skipped", "title": "async with timeout", }, @@ -269,6 +281,7 @@ exports[`json reporter 1`] = ` "duration": 100.50598406791687, "failureMessages": [], "fullName": "suite timeout", + "meta": {}, "status": "passed", "title": "timeout", }, @@ -279,6 +292,7 @@ exports[`json reporter 1`] = ` "duration": 20.184875011444092, "failureMessages": [], "fullName": "suite callback setup success ", + "meta": {}, "status": "passed", "title": "callback setup success ", }, @@ -289,6 +303,7 @@ exports[`json reporter 1`] = ` "duration": 0.33245420455932617, "failureMessages": [], "fullName": "suite callback test success ", + "meta": {}, "status": "passed", "title": "callback test success ", }, @@ -299,6 +314,7 @@ exports[`json reporter 1`] = ` "duration": 19.738605976104736, "failureMessages": [], "fullName": "suite callback setup success done(false)", + "meta": {}, "status": "passed", "title": "callback setup success done(false)", }, @@ -309,6 +325,7 @@ exports[`json reporter 1`] = ` "duration": 0.1923508644104004, "failureMessages": [], "fullName": "suite callback test success done(false)", + "meta": {}, "status": "passed", "title": "callback test success done(false)", }, @@ -318,6 +335,7 @@ exports[`json reporter 1`] = ` ], "failureMessages": [], "fullName": "suite todo test", + "meta": {}, "status": "todo", "title": "todo test", }, @@ -380,6 +398,7 @@ exports[`json reporter with outputFile 2`] = ` "column": 32, "line": 8, }, + "meta": {}, "status": "failed", "title": "Math.sqrt()", }, @@ -390,6 +409,7 @@ exports[`json reporter with outputFile 2`] = ` "duration": 1.0237109661102295, "failureMessages": [], "fullName": "suite JSON", + "meta": {}, "status": "passed", "title": "JSON", }, @@ -399,6 +419,7 @@ exports[`json reporter with outputFile 2`] = ` ], "failureMessages": [], "fullName": "suite async with timeout", + "meta": {}, "status": "skipped", "title": "async with timeout", }, @@ -409,6 +430,7 @@ exports[`json reporter with outputFile 2`] = ` "duration": 100.50598406791687, "failureMessages": [], "fullName": "suite timeout", + "meta": {}, "status": "passed", "title": "timeout", }, @@ -419,6 +441,7 @@ exports[`json reporter with outputFile 2`] = ` "duration": 20.184875011444092, "failureMessages": [], "fullName": "suite callback setup success ", + "meta": {}, "status": "passed", "title": "callback setup success ", }, @@ -429,6 +452,7 @@ exports[`json reporter with outputFile 2`] = ` "duration": 0.33245420455932617, "failureMessages": [], "fullName": "suite callback test success ", + "meta": {}, "status": "passed", "title": "callback test success ", }, @@ -439,6 +463,7 @@ exports[`json reporter with outputFile 2`] = ` "duration": 19.738605976104736, "failureMessages": [], "fullName": "suite callback setup success done(false)", + "meta": {}, "status": "passed", "title": "callback setup success done(false)", }, @@ -449,6 +474,7 @@ exports[`json reporter with outputFile 2`] = ` "duration": 0.1923508644104004, "failureMessages": [], "fullName": "suite callback test success done(false)", + "meta": {}, "status": "passed", "title": "callback test success done(false)", }, @@ -458,6 +484,7 @@ exports[`json reporter with outputFile 2`] = ` ], "failureMessages": [], "fullName": "suite todo test", + "meta": {}, "status": "todo", "title": "todo test", }, @@ -520,6 +547,7 @@ exports[`json reporter with outputFile in non-existing directory 2`] = ` "column": 32, "line": 8, }, + "meta": {}, "status": "failed", "title": "Math.sqrt()", }, @@ -530,6 +558,7 @@ exports[`json reporter with outputFile in non-existing directory 2`] = ` "duration": 1.0237109661102295, "failureMessages": [], "fullName": "suite JSON", + "meta": {}, "status": "passed", "title": "JSON", }, @@ -539,6 +568,7 @@ exports[`json reporter with outputFile in non-existing directory 2`] = ` ], "failureMessages": [], "fullName": "suite async with timeout", + "meta": {}, "status": "skipped", "title": "async with timeout", }, @@ -549,6 +579,7 @@ exports[`json reporter with outputFile in non-existing directory 2`] = ` "duration": 100.50598406791687, "failureMessages": [], "fullName": "suite timeout", + "meta": {}, "status": "passed", "title": "timeout", }, @@ -559,6 +590,7 @@ exports[`json reporter with outputFile in non-existing directory 2`] = ` "duration": 20.184875011444092, "failureMessages": [], "fullName": "suite callback setup success ", + "meta": {}, "status": "passed", "title": "callback setup success ", }, @@ -569,6 +601,7 @@ exports[`json reporter with outputFile in non-existing directory 2`] = ` "duration": 0.33245420455932617, "failureMessages": [], "fullName": "suite callback test success ", + "meta": {}, "status": "passed", "title": "callback test success ", }, @@ -579,6 +612,7 @@ exports[`json reporter with outputFile in non-existing directory 2`] = ` "duration": 19.738605976104736, "failureMessages": [], "fullName": "suite callback setup success done(false)", + "meta": {}, "status": "passed", "title": "callback setup success done(false)", }, @@ -589,6 +623,7 @@ exports[`json reporter with outputFile in non-existing directory 2`] = ` "duration": 0.1923508644104004, "failureMessages": [], "fullName": "suite callback test success done(false)", + "meta": {}, "status": "passed", "title": "callback test success done(false)", }, @@ -598,6 +633,7 @@ exports[`json reporter with outputFile in non-existing directory 2`] = ` ], "failureMessages": [], "fullName": "suite todo test", + "meta": {}, "status": "todo", "title": "todo test", }, @@ -660,6 +696,7 @@ exports[`json reporter with outputFile object 2`] = ` "column": 32, "line": 8, }, + "meta": {}, "status": "failed", "title": "Math.sqrt()", }, @@ -670,6 +707,7 @@ exports[`json reporter with outputFile object 2`] = ` "duration": 1.0237109661102295, "failureMessages": [], "fullName": "suite JSON", + "meta": {}, "status": "passed", "title": "JSON", }, @@ -679,6 +717,7 @@ exports[`json reporter with outputFile object 2`] = ` ], "failureMessages": [], "fullName": "suite async with timeout", + "meta": {}, "status": "skipped", "title": "async with timeout", }, @@ -689,6 +728,7 @@ exports[`json reporter with outputFile object 2`] = ` "duration": 100.50598406791687, "failureMessages": [], "fullName": "suite timeout", + "meta": {}, "status": "passed", "title": "timeout", }, @@ -699,6 +739,7 @@ exports[`json reporter with outputFile object 2`] = ` "duration": 20.184875011444092, "failureMessages": [], "fullName": "suite callback setup success ", + "meta": {}, "status": "passed", "title": "callback setup success ", }, @@ -709,6 +750,7 @@ exports[`json reporter with outputFile object 2`] = ` "duration": 0.33245420455932617, "failureMessages": [], "fullName": "suite callback test success ", + "meta": {}, "status": "passed", "title": "callback test success ", }, @@ -719,6 +761,7 @@ exports[`json reporter with outputFile object 2`] = ` "duration": 19.738605976104736, "failureMessages": [], "fullName": "suite callback setup success done(false)", + "meta": {}, "status": "passed", "title": "callback setup success done(false)", }, @@ -729,6 +772,7 @@ exports[`json reporter with outputFile object 2`] = ` "duration": 0.1923508644104004, "failureMessages": [], "fullName": "suite callback test success done(false)", + "meta": {}, "status": "passed", "title": "callback test success done(false)", }, @@ -738,6 +782,7 @@ exports[`json reporter with outputFile object 2`] = ` ], "failureMessages": [], "fullName": "suite todo test", + "meta": {}, "status": "todo", "title": "todo test", }, @@ -800,6 +845,7 @@ exports[`json reporter with outputFile object in non-existing directory 2`] = ` "column": 32, "line": 8, }, + "meta": {}, "status": "failed", "title": "Math.sqrt()", }, @@ -810,6 +856,7 @@ exports[`json reporter with outputFile object in non-existing directory 2`] = ` "duration": 1.0237109661102295, "failureMessages": [], "fullName": "suite JSON", + "meta": {}, "status": "passed", "title": "JSON", }, @@ -819,6 +866,7 @@ exports[`json reporter with outputFile object in non-existing directory 2`] = ` ], "failureMessages": [], "fullName": "suite async with timeout", + "meta": {}, "status": "skipped", "title": "async with timeout", }, @@ -829,6 +877,7 @@ exports[`json reporter with outputFile object in non-existing directory 2`] = ` "duration": 100.50598406791687, "failureMessages": [], "fullName": "suite timeout", + "meta": {}, "status": "passed", "title": "timeout", }, @@ -839,6 +888,7 @@ exports[`json reporter with outputFile object in non-existing directory 2`] = ` "duration": 20.184875011444092, "failureMessages": [], "fullName": "suite callback setup success ", + "meta": {}, "status": "passed", "title": "callback setup success ", }, @@ -849,6 +899,7 @@ exports[`json reporter with outputFile object in non-existing directory 2`] = ` "duration": 0.33245420455932617, "failureMessages": [], "fullName": "suite callback test success ", + "meta": {}, "status": "passed", "title": "callback test success ", }, @@ -859,6 +910,7 @@ exports[`json reporter with outputFile object in non-existing directory 2`] = ` "duration": 19.738605976104736, "failureMessages": [], "fullName": "suite callback setup success done(false)", + "meta": {}, "status": "passed", "title": "callback setup success done(false)", }, @@ -869,6 +921,7 @@ exports[`json reporter with outputFile object in non-existing directory 2`] = ` "duration": 0.1923508644104004, "failureMessages": [], "fullName": "suite callback test success done(false)", + "meta": {}, "status": "passed", "title": "callback test success done(false)", }, @@ -878,6 +931,7 @@ exports[`json reporter with outputFile object in non-existing directory 2`] = ` ], "failureMessages": [], "fullName": "suite todo test", + "meta": {}, "status": "todo", "title": "todo test", }, diff --git a/test/reporters/tests/merge-reports.test.ts b/test/reporters/tests/merge-reports.test.ts index f07724a9236f..be73e8dc03db 100644 --- a/test/reporters/tests/merge-reports.test.ts +++ b/test/reporters/tests/merge-reports.test.ts @@ -163,6 +163,7 @@ test('merge reports', async () => { "ancestorTitles": [], "failureMessages": [], "fullName": "test 1-1", + "meta": {}, "status": "passed", "title": "test 1-1", }, @@ -173,6 +174,7 @@ test('merge reports', async () => { at /fixtures/merge-reports/first.test.ts:15:13", ], "fullName": "test 1-2", + "meta": {}, "status": "failed", "title": "test 1-2", }, @@ -192,6 +194,7 @@ test('merge reports', async () => { at /fixtures/merge-reports/second.test.ts:5:13", ], "fullName": "test 2-1", + "meta": {}, "status": "failed", "title": "test 2-1", }, @@ -201,6 +204,7 @@ test('merge reports', async () => { ], "failureMessages": [], "fullName": "group test 2-2", + "meta": {}, "status": "passed", "title": "test 2-2", }, @@ -210,6 +214,7 @@ test('merge reports', async () => { ], "failureMessages": [], "fullName": "group test 2-3", + "meta": {}, "status": "passed", "title": "test 2-3", },