From 00c432fad6c40b4ff21cc45b18e64f29816062db Mon Sep 17 00:00:00 2001 From: "Hikaru Terazono (3c1u)" <3c1u@tohkani.com> Date: Wed, 23 Aug 2023 22:15:19 +0900 Subject: [PATCH] fix: report file error as a on JUnit (#3997) --- .eslintignore | 1 + packages/vitest/src/node/reporters/junit.ts | 17 +++++++++++++++++ .../fixtures/with-syntax-error.test.js | 4 ++++ test/reporters/tests/junit.test.ts | 17 +++++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 test/reporters/fixtures/with-syntax-error.test.js diff --git a/.eslintignore b/.eslintignore index 6771603144e1..f88ccc31cbbd 100644 --- a/.eslintignore +++ b/.eslintignore @@ -10,3 +10,4 @@ coverage docs/.vitepress/cache/deps/*.* test/core/src/self test/workspaces/results.json +test/reporters/fixtures/with-syntax-error.test.js diff --git a/packages/vitest/src/node/reporters/junit.ts b/packages/vitest/src/node/reporters/junit.ts index c8b4e0460434..68e6168b55e2 100644 --- a/packages/vitest/src/node/reporters/junit.ts +++ b/packages/vitest/src/node/reporters/junit.ts @@ -216,6 +216,23 @@ export class JUnitReporter implements Reporter { skipped: 0, }) + // If there are no tests, but the file failed to load, we still want to report it as a failure + if (tasks.length === 0 && file.result?.state === 'fail') { + stats.failures = 1 + + tasks.push({ + id: file.id, + type: 'test', + name: file.name, + mode: 'run', + result: file.result, + meta: {}, + // NOTE: not used in JUnitReporter + context: null as any, + suite: null as any, + } satisfies Task) + } + return { ...file, tasks, diff --git a/test/reporters/fixtures/with-syntax-error.test.js b/test/reporters/fixtures/with-syntax-error.test.js new file mode 100644 index 000000000000..864bf73ebe05 --- /dev/null +++ b/test/reporters/fixtures/with-syntax-error.test.js @@ -0,0 +1,4 @@ +// NOTE: This file is intentionally not valid JavaScript. + +it('should fail', () => { +) diff --git a/test/reporters/tests/junit.test.ts b/test/reporters/tests/junit.test.ts index a76a61aad0e3..99d1b39395d5 100644 --- a/test/reporters/tests/junit.test.ts +++ b/test/reporters/tests/junit.test.ts @@ -1,7 +1,11 @@ import type { Suite, Task, TaskResult } from 'vitest' import { expect, test } from 'vitest' +import { resolve } from 'pathe' +import { runVitest } from '../../test-utils' import { getDuration } from '../../../packages/vitest/src/node/reporters/junit' +const root = resolve(__dirname, '../fixtures') + test('calc the duration used by junit', () => { const result: TaskResult = { state: 'pass', duration: 0 } const suite: Suite = { @@ -34,3 +38,16 @@ test('calc the duration used by junit', () => { result.duration = 12001 expect(getDuration(task)).toBe('12.001') }) + +test('emits if a test has a syntax error', async () => { + const { stdout } = await runVitest({ reporters: 'junit', root }, ['with-syntax-error']) + + let xml = stdout + + // clear timestamp and hostname + xml = xml.replace(/timestamp="[^"]+"/, 'timestamp="TIMESTAMP"') + xml = xml.replace(/hostname="[^"]+"/, 'hostname="HOSTNAME"') + + expect(xml).toContain('') + expect(xml).toContain('