From c997937bf37c0a39883bcf29d5c4188b96ae3924 Mon Sep 17 00:00:00 2001 From: Haozheng Li <2914034404@qq.com> Date: Thu, 1 Aug 2024 16:56:18 +0800 Subject: [PATCH] fix(vitest): update json reporter output (#6064) --- docs/guide/reporters.md | 14 +++--- packages/vitest/src/node/reporters/json.ts | 12 +++-- .../__snapshots__/reporters.spec.ts.snap | 48 +++++++++---------- test/reporters/tests/merge-reports.test.ts | 4 +- 4 files changed, 40 insertions(+), 38 deletions(-) diff --git a/docs/guide/reporters.md b/docs/guide/reporters.md index 70907c03c23d..e18e974d4116 100644 --- a/docs/guide/reporters.md +++ b/docs/guide/reporters.md @@ -284,15 +284,15 @@ Example of a JSON report: ```json { - "numTotalTestSuites": 1, - "numPassedTestSuites": 0, + "numTotalTestSuites": 4, + "numPassedTestSuites": 2, "numFailedTestSuites": 1, - "numPendingTestSuites": 0, - "numTotalTests": 1, - "numPassedTests": 0, + "numPendingTestSuites": 1, + "numTotalTests": 4, + "numPassedTests": 1, "numFailedTests": 1, - "numPendingTests": 0, - "numTodoTests": 0, + "numPendingTests": 1, + "numTodoTests": 1, "startTime": 1697737019307, "success": false, "testResults": [ diff --git a/packages/vitest/src/node/reporters/json.ts b/packages/vitest/src/node/reporters/json.ts index cdad5ed22bd1..b9b335f7ebe5 100644 --- a/packages/vitest/src/node/reporters/json.ts +++ b/packages/vitest/src/node/reporters/json.ts @@ -91,17 +91,19 @@ export class JsonReporter implements Reporter { const numTotalTestSuites = suites.length const tests = getTests(files) const numTotalTests = tests.length - const numFailedTestSuites = suites.filter(s => s.result?.errors).length - const numPassedTestSuites = numTotalTestSuites - numFailedTestSuites + + const numFailedTestSuites = suites.filter(s => s.result?.state === 'fail').length const numPendingTestSuites = suites.filter( - s => s.result?.state === 'run', + s => s.result?.state === 'run' || s.mode === 'todo', ).length + const numPassedTestSuites = numTotalTestSuites - numFailedTestSuites - numPendingTestSuites + const numFailedTests = tests.filter( t => t.result?.state === 'fail', ).length - const numPassedTests = numTotalTests - numFailedTests + const numPassedTests = tests.filter(t => t.result?.state === 'pass').length const numPendingTests = tests.filter( - t => t.result?.state === 'run', + t => t.result?.state === 'run' || t.mode === 'skip' || t.result?.state === 'skip', ).length const numTodoTests = tests.filter(t => t.mode === 'todo').length const testResults: Array = [] diff --git a/test/reporters/tests/__snapshots__/reporters.spec.ts.snap b/test/reporters/tests/__snapshots__/reporters.spec.ts.snap index 088b1b17458f..32d3fc3f7ab1 100644 --- a/test/reporters/tests/__snapshots__/reporters.spec.ts.snap +++ b/test/reporters/tests/__snapshots__/reporters.spec.ts.snap @@ -64,12 +64,12 @@ exports[`JUnit reporter with outputFile object in non-existing directory 2`] = ` exports[`json reporter (no outputFile entry) 1`] = ` { - "numFailedTestSuites": 0, + "numFailedTestSuites": 1, "numFailedTests": 1, - "numPassedTestSuites": 2, - "numPassedTests": 8, + "numPassedTestSuites": 1, + "numPassedTests": 6, "numPendingTestSuites": 0, - "numPendingTests": 0, + "numPendingTests": 1, "numTodoTests": 1, "numTotalTestSuites": 2, "numTotalTests": 9, @@ -208,12 +208,12 @@ exports[`json reporter (no outputFile entry) 1`] = ` exports[`json reporter 1`] = ` { - "numFailedTestSuites": 0, + "numFailedTestSuites": 1, "numFailedTests": 1, - "numPassedTestSuites": 2, - "numPassedTests": 8, + "numPassedTestSuites": 1, + "numPassedTests": 6, "numPendingTestSuites": 0, - "numPendingTests": 0, + "numPendingTests": 1, "numTodoTests": 1, "numTotalTestSuites": 2, "numTotalTests": 9, @@ -357,12 +357,12 @@ exports[`json reporter with outputFile 1`] = ` exports[`json reporter with outputFile 2`] = ` { - "numFailedTestSuites": 0, + "numFailedTestSuites": 1, "numFailedTests": 1, - "numPassedTestSuites": 2, - "numPassedTests": 8, + "numPassedTestSuites": 1, + "numPassedTests": 6, "numPendingTestSuites": 0, - "numPendingTests": 0, + "numPendingTests": 1, "numTodoTests": 1, "numTotalTestSuites": 2, "numTotalTests": 9, @@ -506,12 +506,12 @@ exports[`json reporter with outputFile in non-existing directory 1`] = ` exports[`json reporter with outputFile in non-existing directory 2`] = ` { - "numFailedTestSuites": 0, + "numFailedTestSuites": 1, "numFailedTests": 1, - "numPassedTestSuites": 2, - "numPassedTests": 8, + "numPassedTestSuites": 1, + "numPassedTests": 6, "numPendingTestSuites": 0, - "numPendingTests": 0, + "numPendingTests": 1, "numTodoTests": 1, "numTotalTestSuites": 2, "numTotalTests": 9, @@ -655,12 +655,12 @@ exports[`json reporter with outputFile object 1`] = ` exports[`json reporter with outputFile object 2`] = ` { - "numFailedTestSuites": 0, + "numFailedTestSuites": 1, "numFailedTests": 1, - "numPassedTestSuites": 2, - "numPassedTests": 8, + "numPassedTestSuites": 1, + "numPassedTests": 6, "numPendingTestSuites": 0, - "numPendingTests": 0, + "numPendingTests": 1, "numTodoTests": 1, "numTotalTestSuites": 2, "numTotalTests": 9, @@ -804,12 +804,12 @@ exports[`json reporter with outputFile object in non-existing directory 1`] = ` exports[`json reporter with outputFile object in non-existing directory 2`] = ` { - "numFailedTestSuites": 0, + "numFailedTestSuites": 1, "numFailedTests": 1, - "numPassedTestSuites": 2, - "numPassedTests": 8, + "numPassedTestSuites": 1, + "numPassedTests": 6, "numPendingTestSuites": 0, - "numPendingTests": 0, + "numPendingTests": 1, "numTodoTests": 1, "numTotalTestSuites": 2, "numTotalTests": 9, diff --git a/test/reporters/tests/merge-reports.test.ts b/test/reporters/tests/merge-reports.test.ts index 70de1b63fba0..b66ccd7c7c9d 100644 --- a/test/reporters/tests/merge-reports.test.ts +++ b/test/reporters/tests/merge-reports.test.ts @@ -129,9 +129,9 @@ test('merge reports', async () => { expect(json).toMatchInlineSnapshot(` { - "numFailedTestSuites": 0, + "numFailedTestSuites": 2, "numFailedTests": 2, - "numPassedTestSuites": 3, + "numPassedTestSuites": 1, "numPassedTests": 3, "numPendingTestSuites": 0, "numPendingTests": 0,