Skip to content

Commit

Permalink
feat!: add meta to json output (#5802)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Jun 3, 2024
1 parent 34a310d commit dd754c1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/advanced/metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
:::
Expand Down
4 changes: 3 additions & 1 deletion packages/vitest/src/node/reporters/json.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -30,6 +30,7 @@ export interface JsonAssertionResult {
fullName: string
status: Status
title: string
meta: TaskMeta
duration?: Milliseconds | null
failureMessages: Array<string> | null
location?: Callsite | null
Expand Down Expand Up @@ -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
})

Expand Down
1 change: 1 addition & 0 deletions test/reporters/tests/__snapshots__/json.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ exports[`json reporter > generates correct report 1`] = `
"column": 1,
"line": 5,
},
"meta": {},
"status": "failed",
"title": "should fail",
}
Expand Down
Loading

0 comments on commit dd754c1

Please sign in to comment.