From ce9319563a3456319623f9fcb86ad316db59d0ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Brzezi=C5=84ski?= <33814584+brzezinskimarcin@users.noreply.github.com> Date: Mon, 5 Dec 2022 13:18:46 +0100 Subject: [PATCH] fix: correct test files status in json reporter (fix #2417) (#2419) * fix: correct test files status in json reporter (fix #2417) * fix: apply a non-breaking fix --- packages/vitest/src/node/reporters/json.ts | 10 ++++----- .../fixtures/all-passing-or-skipped.test.ts | 9 ++++++++ test/reporters/fixtures/all-skipped.test.ts | 9 ++++++++ test/reporters/fixtures/some-failing.test.ts | 9 ++++++++ test/reporters/tests/json.test.ts | 21 +++++++++++++++++++ 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 test/reporters/fixtures/all-passing-or-skipped.test.ts create mode 100644 test/reporters/fixtures/all-skipped.test.ts create mode 100644 test/reporters/fixtures/some-failing.test.ts diff --git a/packages/vitest/src/node/reporters/json.ts b/packages/vitest/src/node/reporters/json.ts index bd5d8318dcbf..ba84ae1456c3 100644 --- a/packages/vitest/src/node/reporters/json.ts +++ b/packages/vitest/src/node/reporters/json.ts @@ -124,12 +124,10 @@ export class JsonReporter implements Reporter { assertionResults, startTime, endTime, - status: tests.every(t => - t.result?.state === 'pass' - || t.result?.state === 'skip' - || t.result?.state === 'todo') - ? 'passed' - : 'failed', + status: tests.some(t => + t.result?.state === 'fail') + ? 'failed' + : 'passed', message: file.result?.error?.message ?? '', name: file.filepath, }) diff --git a/test/reporters/fixtures/all-passing-or-skipped.test.ts b/test/reporters/fixtures/all-passing-or-skipped.test.ts new file mode 100644 index 000000000000..ba5b9149901f --- /dev/null +++ b/test/reporters/fixtures/all-passing-or-skipped.test.ts @@ -0,0 +1,9 @@ +import { expect, test } from 'vitest' + +test('2 + 3 = 5', () => { + expect(2 + 3).toBe(5) +}) + +test.skip('3 + 3 = 6', () => { + expect(3 + 3).toBe(6) +}) diff --git a/test/reporters/fixtures/all-skipped.test.ts b/test/reporters/fixtures/all-skipped.test.ts new file mode 100644 index 000000000000..c604922dd642 --- /dev/null +++ b/test/reporters/fixtures/all-skipped.test.ts @@ -0,0 +1,9 @@ +import { expect, test } from 'vitest' + +test.todo('2 + 3 = 5', () => { + expect(2 + 3).toBe(5) +}) + +test.skip('3 + 3 = 6', () => { + expect(3 + 3).toBe(6) +}) diff --git a/test/reporters/fixtures/some-failing.test.ts b/test/reporters/fixtures/some-failing.test.ts new file mode 100644 index 000000000000..37d56a49a1c2 --- /dev/null +++ b/test/reporters/fixtures/some-failing.test.ts @@ -0,0 +1,9 @@ +import { expect, test } from 'vitest' + +test('2 + 3 = 5', () => { + expect(2 + 3).toBe(5) +}) + +test('3 + 3 = 7', () => { + expect(3 + 3).toBe(7) +}) diff --git a/test/reporters/tests/json.test.ts b/test/reporters/tests/json.test.ts index 3963d9dfc405..217e56c30546 100644 --- a/test/reporters/tests/json.test.ts +++ b/test/reporters/tests/json.test.ts @@ -27,4 +27,25 @@ describe('json reporter', async () => { delete result.duration expect(result).toMatchSnapshot() }, 40000) + + it.skipIf(skip).each([ + ['passed', 'all-passing-or-skipped'], + ['passed', 'all-skipped'], + ['failed', 'some-failing'], + ])('resolves to "%s" status for test file "%s"', async (expected, file) => { + const { stdout } = await execa('npx', ['vitest', 'run', file, '--reporter=json'], { + cwd: root, + env: { + ...process.env, + CI: 'true', + NO_COLOR: 'true', + }, + stdio: 'pipe', + }).catch(e => e) + + const data = JSON.parse(stdout) + + expect(data.testResults).toHaveLength(1) + expect(data.testResults[0].status).toBe(expected) + }, 40000) })