Skip to content

Commit 61de1eb

Browse files
committed
refactor: rename filterMetaField to filterMeta and support truthy/falsy
1 parent f1ce4af commit 61de1eb

3 files changed

Lines changed: 17 additions & 13 deletions

File tree

docs/guide/reporters.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,14 +417,14 @@ Example of a JSON report:
417417
Since Vitest 3, the JSON reporter includes coverage information in `coverageMap` if coverage is enabled.
418418
:::
419419

420-
The `meta` field in each assertion result can be filtered via the `filterMetaField` reporter option. It receives the key and value of each field and should return `false` to exclude the field from the report:
420+
The `meta` field in each assertion result can be filtered via the `filterMeta` reporter option. It receives the key and value of each field and should return a falsy value to exclude the field from the report:
421421

422422
```ts
423423
export default defineConfig({
424424
test: {
425425
reporters: [
426426
['json', {
427-
filterMetaField: (key, value) => key !== 'internalField',
427+
filterMeta: (key, value) => key !== 'internalField',
428428
}]
429429
]
430430
},

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export interface JsonTestResults {
7474

7575
export interface JsonOptions {
7676
outputFile?: string
77-
filterMetaField?: (key: string, value: unknown) => boolean
77+
filterMeta?: (key: string, value: unknown) => unknown
7878
}
7979

8080
export class JsonReporter implements Reporter {
@@ -122,7 +122,7 @@ export class JsonReporter implements Reporter {
122122
const testResults: Array<JsonTestResult> = []
123123

124124
const success = !!(files.length > 0 || this.ctx.config.passWithNoTests) && numFailedTestSuites === 0 && numFailedTests === 0
125-
const { filterMetaField } = this.options
125+
const { filterMeta } = this.options
126126

127127
for (const file of files) {
128128
const tests = getTests([file])
@@ -163,12 +163,16 @@ export class JsonReporter implements Reporter {
163163
failureMessages:
164164
t.result?.errors?.map(e => e.stack || e.message) || [],
165165
location: t.location,
166-
meta: filterMetaField
167-
? Object.fromEntries(
168-
Object.entries(t.meta).filter(([key, value]) =>
169-
filterMetaField(key, value),
170-
),
171-
) as TaskMeta
166+
meta: filterMeta
167+
? (() => {
168+
const filtered: Record<string, unknown> = {}
169+
for (const [key, value] of Object.entries(t.meta)) {
170+
if (filterMeta(key, value)) {
171+
filtered[key] = value
172+
}
173+
}
174+
return filtered
175+
})()
172176
: t.meta,
173177
tags: t.tags || [],
174178
} satisfies JsonAssertionResult

test/cli/test/reporters/json.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describe('json reporter', async () => {
139139
expect(data.testResults[0].status).toBe(expected)
140140
})
141141

142-
it('includes all meta fields when filterMetaField is not set', async () => {
142+
it('includes all meta fields when filterMeta is not set', async () => {
143143
const { stdout } = await runVitest({
144144
reporters: 'json',
145145
root,
@@ -153,10 +153,10 @@ describe('json reporter', async () => {
153153
expect(passing.meta).toEqual({ custom: 'Passing test added this' })
154154
})
155155

156-
it('filterMetaField filters meta fields by key', async () => {
156+
it('filterMeta filters meta fields by key', async () => {
157157
const { stdout } = await runVitest({
158158
reporters: [['json', {
159-
filterMetaField: key => key !== 'custom',
159+
filterMeta: key => key !== 'custom',
160160
}]],
161161
root,
162162
include: ['**/json-meta.test.ts'],

0 commit comments

Comments
 (0)