Skip to content

Commit dd754c1

Browse files
authored
feat!: add meta to json output (#5802)
1 parent 34a310d commit dd754c1

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

docs/advanced/metadata.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Vitest uses different methods to communicate with the Node.js process.
4848
- 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
4949
- If Vitest runs tests in the browser, the data will be stringified using [flatted](https://www.npmjs.com/package/flatted) package
5050

51-
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.
51+
This property is also present on every test in the `json` reporter, so make sure that data can be serialized into JSON.
5252

5353
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.
5454
:::

packages/vitest/src/node/reporters/json.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { existsSync, promises as fs } from 'node:fs'
22
import { dirname, resolve } from 'pathe'
33
import type { Vitest } from '../../node'
4-
import type { File, Reporter, SnapshotSummary, Suite, TaskState } from '../../types'
4+
import type { File, Reporter, SnapshotSummary, Suite, TaskMeta, TaskState } from '../../types'
55
import { getSuites, getTests } from '../../utils'
66
import { getOutputFile } from '../../utils/config-helpers'
77

@@ -30,6 +30,7 @@ export interface JsonAssertionResult {
3030
fullName: string
3131
status: Status
3232
title: string
33+
meta: TaskMeta
3334
duration?: Milliseconds | null
3435
failureMessages: Array<string> | null
3536
location?: Callsite | null
@@ -123,6 +124,7 @@ export class JsonReporter implements Reporter {
123124
duration: t.result?.duration,
124125
failureMessages: t.result?.errors?.map(e => e.stack || e.message) || [],
125126
location: t.location,
127+
meta: t.meta,
126128
} satisfies JsonAssertionResult
127129
})
128130

test/reporters/tests/__snapshots__/json.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ exports[`json reporter > generates correct report 1`] = `
1212
"column": 1,
1313
"line": 5,
1414
},
15+
"meta": {},
1516
"status": "failed",
1617
"title": "should fail",
1718
}

0 commit comments

Comments
 (0)