From 46383d3d0f6f4aad6a6010ce9663cf7648f3953a Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 26 Apr 2024 19:07:04 +0900 Subject: [PATCH 1/8] fix(reporter/junit): use same error formatting as default reporter --- packages/vitest/src/node/reporters/github-actions.ts | 2 +- packages/vitest/src/node/reporters/junit.ts | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/node/reporters/github-actions.ts b/packages/vitest/src/node/reporters/github-actions.ts index 8863bb7bbd50..ac751e45d6d8 100644 --- a/packages/vitest/src/node/reporters/github-actions.ts +++ b/packages/vitest/src/node/reporters/github-actions.ts @@ -65,7 +65,7 @@ export class GithubActionsReporter implements Reporter { // use Logger with custom Console to extract messgage from `processError` util // TODO: maybe refactor `processError` to require single function `(message: string) => void` instead of full Logger? -async function printErrorWrapper(error: unknown, ctx: Vitest, project: WorkspaceProject) { +export async function printErrorWrapper(error: unknown, ctx: Vitest, project: WorkspaceProject) { let output = '' const writable = new Writable({ write(chunk, _encoding, callback) { diff --git a/packages/vitest/src/node/reporters/junit.ts b/packages/vitest/src/node/reporters/junit.ts index 6d1c5395bd29..bf6f24dd25fa 100644 --- a/packages/vitest/src/node/reporters/junit.ts +++ b/packages/vitest/src/node/reporters/junit.ts @@ -11,6 +11,7 @@ import { parseErrorStacktrace } from '../../utils/source-map' import { F_POINTER } from '../../utils/figures' import { getOutputFile } from '../../utils/config-helpers' import { IndentedLogger } from './renderers/indented-logger' +import { printErrorWrapper } from './github-actions' export interface JUnitOptions { outputFile?: string @@ -205,7 +206,13 @@ export class JUnitReporter implements Reporter { if (!error) return - await this.writeErrorDetails(task, error) + const result = await printErrorWrapper( + error, + this.ctx, + this.ctx.getProjectByTaskId(task.id), + ) + // TODO: should strip color? + await this.baseLog(escapeXML(result.output)) }) } } From 4d035ac9656cd3a695ebe72e79fe3245dd66e8d4 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 26 Apr 2024 19:16:24 +0900 Subject: [PATCH 2/8] chore: strip color --- packages/vitest/src/node/reporters/junit.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/node/reporters/junit.ts b/packages/vitest/src/node/reporters/junit.ts index bf6f24dd25fa..30c7f3b6f86c 100644 --- a/packages/vitest/src/node/reporters/junit.ts +++ b/packages/vitest/src/node/reporters/junit.ts @@ -5,6 +5,7 @@ import { dirname, relative, resolve } from 'pathe' import type { Task } from '@vitest/runner' import type { ErrorWithDiff } from '@vitest/utils' import { getSuites } from '@vitest/runner/utils' +import stripAnsi from 'strip-ansi' import type { Vitest } from '../../node' import type { Reporter } from '../../types/reporter' import { parseErrorStacktrace } from '../../utils/source-map' @@ -211,8 +212,7 @@ export class JUnitReporter implements Reporter { this.ctx, this.ctx.getProjectByTaskId(task.id), ) - // TODO: should strip color? - await this.baseLog(escapeXML(result.output)) + await this.baseLog(escapeXML(stripAnsi(result.output))) }) } } From 10e707cc68d20282507f9f5b43ff0701ca26cd70 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 26 Apr 2024 19:24:04 +0900 Subject: [PATCH 3/8] chore: trim --- packages/vitest/src/node/reporters/junit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/node/reporters/junit.ts b/packages/vitest/src/node/reporters/junit.ts index 30c7f3b6f86c..650cc5937320 100644 --- a/packages/vitest/src/node/reporters/junit.ts +++ b/packages/vitest/src/node/reporters/junit.ts @@ -212,7 +212,7 @@ export class JUnitReporter implements Reporter { this.ctx, this.ctx.getProjectByTaskId(task.id), ) - await this.baseLog(escapeXML(stripAnsi(result.output))) + await this.baseLog(escapeXML(stripAnsi(result.output.trim()))) }) } } From ed781d54322f873b7be71019da753d50def9cf11 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 26 Apr 2024 19:26:01 +0900 Subject: [PATCH 4/8] chore: remove unused --- packages/vitest/src/node/reporters/junit.ts | 28 --------------------- 1 file changed, 28 deletions(-) diff --git a/packages/vitest/src/node/reporters/junit.ts b/packages/vitest/src/node/reporters/junit.ts index 650cc5937320..f4df3b43b422 100644 --- a/packages/vitest/src/node/reporters/junit.ts +++ b/packages/vitest/src/node/reporters/junit.ts @@ -3,13 +3,10 @@ import { hostname } from 'node:os' import { dirname, relative, resolve } from 'pathe' import type { Task } from '@vitest/runner' -import type { ErrorWithDiff } from '@vitest/utils' import { getSuites } from '@vitest/runner/utils' import stripAnsi from 'strip-ansi' import type { Vitest } from '../../node' import type { Reporter } from '../../types/reporter' -import { parseErrorStacktrace } from '../../utils/source-map' -import { F_POINTER } from '../../utils/figures' import { getOutputFile } from '../../utils/config-helpers' import { IndentedLogger } from './renderers/indented-logger' import { printErrorWrapper } from './github-actions' @@ -142,31 +139,6 @@ export class JUnitReporter implements Reporter { await this.logger.log(``) } - async writeErrorDetails(task: Task, error: ErrorWithDiff): Promise { - const errorName = error.name ?? error.nameStr ?? 'Unknown Error' - const errorDetails = `${errorName}: ${error.message}` - - // Be sure to escape any XML in the error Details - await this.baseLog(escapeXML(errorDetails)) - - const project = this.ctx.getProjectByTaskId(task.id) - const stack = parseErrorStacktrace(error, { - getSourceMap: file => project.getBrowserSourceMapModuleById(file), - frameFilter: this.ctx.config.onStackTrace, - }) - - // TODO: This is same as printStack but without colors. Find a way to reuse code. - for (const frame of stack) { - const path = relative(this.ctx.config.root, frame.file) - - await this.baseLog(escapeXML(` ${F_POINTER} ${[frame.method, `${path}:${frame.line}:${frame.column}`].filter(Boolean).join(' ')}`)) - - // reached at test file, skip the follow stack - if (frame.file in this.ctx.state.filesMap) - break - } - } - async writeLogs(task: Task, type: 'err' | 'out'): Promise { if (task.logs == null || task.logs.length === 0) return From cecd5b9fee121e454cee94751dc08907f6f019b3 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 27 Apr 2024 09:40:12 +0900 Subject: [PATCH 5/8] test: add test --- test/reporters/fixtures/error.test.ts | 46 ++++++++++++ .../tests/__snapshots__/junit.test.ts.snap | 74 +++++++++++++++++++ test/reporters/tests/junit.test.ts | 5 ++ test/reporters/tests/reporters.spec.ts | 14 ++-- 4 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 test/reporters/fixtures/error.test.ts diff --git a/test/reporters/fixtures/error.test.ts b/test/reporters/fixtures/error.test.ts new file mode 100644 index 000000000000..4a73c4d136b7 --- /dev/null +++ b/test/reporters/fixtures/error.test.ts @@ -0,0 +1,46 @@ +import { afterAll, it, expect } from "vitest"; + +afterAll(() => { + throwSuite() +}) + +it('stack', () => { + throwDeep() +}) + +it('diff', () => { + expect({ hello: 'x' }).toEqual({ hello: 'y' }) +}) + +it('unhandled', () => { + (async () => throwSimple())() +}) + +it('no name object', () => { + throw { noName: 'hi' }; +}); + +it('string', () => { + throw "hi"; +}); + +it('number', () => { + throw 1234; +}); + +it('number name object', () => { + throw { name: 1234 }; +}); + + +function throwDeep() { + throwSimple() +} + +function throwSimple() { + throw new Error('throwSimple') +} + +function throwSuite() { + throw new Error('throwSuite') +} diff --git a/test/reporters/tests/__snapshots__/junit.test.ts.snap b/test/reporters/tests/__snapshots__/junit.test.ts.snap index a6c51a996401..1744f14468fe 100644 --- a/test/reporters/tests/__snapshots__/junit.test.ts.snap +++ b/test/reporters/tests/__snapshots__/junit.test.ts.snap @@ -34,3 +34,77 @@ Error: afterAll error " `; + +exports[`format error 1`] = ` +" + + + + +Error: throwSimple + ❯ throwSimple error.test.ts:41:9 + ❯ throwDeep error.test.ts:37:3 + ❯ error.test.ts:8:3 + + + + +AssertionError: expected { hello: 'x' } to deeply equal { hello: 'y' } + +- Expected ++ Received + + Object { +- "hello": "y", ++ "hello": "x", + } + + ❯ error.test.ts:12:26 + + + + + + +{ + noName: 'hi', + expected: 'undefined', + actual: 'undefined', + stacks: [] +} +⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ +Serialized Error: { noName: 'hi' } + + + + +Unknown Error: hi + + + + +Unknown Error: 1234 + + + + +{ + name: 1234, + nameStr: '1234', + expected: 'undefined', + actual: 'undefined', + stacks: [] +} + + + + +Error: throwSuite + ❯ throwSuite error.test.ts:45:9 + ❯ error.test.ts:4:3 + + + + +" +`; diff --git a/test/reporters/tests/junit.test.ts b/test/reporters/tests/junit.test.ts index 72cdcbd4ab98..07b2698ce050 100644 --- a/test/reporters/tests/junit.test.ts +++ b/test/reporters/tests/junit.test.ts @@ -57,6 +57,11 @@ test('emits when beforeAll/afterAll failed', async () => { expect(xml).toMatchSnapshot() }) +test('format error', async () => { + const { stdout } = await runVitest({ reporters: 'junit', root }, ['error.test.ts']) + expect(stabilizeReport(stdout)).toMatchSnapshot() +}) + test('write testsuite name relative to root config', async () => { const { stdout } = await runVitest({ reporters: 'junit', root: './fixtures/better-testsuite-name' }) diff --git a/test/reporters/tests/reporters.spec.ts b/test/reporters/tests/reporters.spec.ts index 6245b034723c..d98671193a0a 100644 --- a/test/reporters/tests/reporters.spec.ts +++ b/test/reporters/tests/reporters.spec.ts @@ -39,7 +39,7 @@ test('tap-flat reporter', async () => { expect(context.output).toMatchSnapshot() }) -test('JUnit reporter', async () => { +test.skip('JUnit reporter', async () => { // Arrange const reporter = new JUnitReporter({}) const context = getContext() @@ -58,7 +58,7 @@ test('JUnit reporter', async () => { expect(context.output).toMatchSnapshot() }) -test('JUnit reporter (no outputFile entry)', async () => { +test.skip('JUnit reporter (no outputFile entry)', async () => { // Arrange const reporter = new JUnitReporter({}) const context = getContext() @@ -78,7 +78,7 @@ test('JUnit reporter (no outputFile entry)', async () => { expect(context.output).toMatchSnapshot() }) -test('JUnit reporter with outputFile', async () => { +test.skip('JUnit reporter with outputFile', async () => { // Arrange const reporter = new JUnitReporter({}) const outputFile = resolve('report.xml') @@ -104,7 +104,7 @@ test('JUnit reporter with outputFile', async () => { rmSync(outputFile) }) -test('JUnit reporter with outputFile with XML in error message', async () => { +test.skip('JUnit reporter with outputFile with XML in error message', async () => { // Arrange const reporter = new JUnitReporter({}) const outputFile = resolve('report_escape_msg_xml.xml') @@ -133,7 +133,7 @@ test('JUnit reporter with outputFile with XML in error message', async () => { rmSync(outputFile) }) -test('JUnit reporter with outputFile object', async () => { +test.skip('JUnit reporter with outputFile object', async () => { // Arrange const reporter = new JUnitReporter({}) const outputFile = resolve('report_object.xml') @@ -161,7 +161,7 @@ test('JUnit reporter with outputFile object', async () => { rmSync(outputFile) }) -test('JUnit reporter with outputFile in non-existing directory', async () => { +test.skip('JUnit reporter with outputFile in non-existing directory', async () => { // Arrange const reporter = new JUnitReporter({}) const rootDirectory = resolve('junitReportDirectory') @@ -188,7 +188,7 @@ test('JUnit reporter with outputFile in non-existing directory', async () => { rmSync(outputFile) }) -test('JUnit reporter with outputFile object in non-existing directory', async () => { +test.skip('JUnit reporter with outputFile object in non-existing directory', async () => { // Arrange const reporter = new JUnitReporter({}) const rootDirectory = resolve('junitReportDirectory_object') From 59e7110b01ea60001aed3fa4a18f4e17d8ee160e Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 27 Apr 2024 09:56:26 +0900 Subject: [PATCH 6/8] refactor: move code --- packages/vitest/src/node/error.ts | 19 ++++++++++++++- .../src/node/reporters/github-actions.ts | 23 ++----------------- packages/vitest/src/node/reporters/junit.ts | 4 ++-- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/packages/vitest/src/node/error.ts b/packages/vitest/src/node/error.ts index d099bc482bb9..6eee0ad8872d 100644 --- a/packages/vitest/src/node/error.ts +++ b/packages/vitest/src/node/error.ts @@ -1,5 +1,6 @@ /* eslint-disable prefer-template */ import { existsSync, readFileSync } from 'node:fs' +import { Writable } from 'node:stream' import { normalize, relative } from 'pathe' import c from 'picocolors' import cliTruncate from 'cli-truncate' @@ -13,7 +14,7 @@ import { TypeCheckError } from '../typecheck/typechecker' import { isPrimitive } from '../utils' import type { Vitest } from './core' import { divider } from './reporters/renderers/utils' -import type { Logger } from './logger' +import { Logger } from './logger' import type { WorkspaceProject } from './workspace' interface PrintErrorOptions { @@ -27,6 +28,22 @@ interface PrintErrorResult { nearest?: ParsedStack } +// use Logger with custom Console to capture entire error printing +export async function captuerPrintError(error: unknown, ctx: Vitest, project: WorkspaceProject) { + let output = '' + const writable = new Writable({ + write(chunk, _encoding, callback) { + output += String(chunk) + callback() + }, + }) + const result = await printError(error, project, { + showCodeFrame: false, + logger: new Logger(ctx, writable, writable), + }) + return { nearest: result?.nearest, output } +} + export async function printError(error: unknown, project: WorkspaceProject | undefined, options: PrintErrorOptions): Promise { const { showCodeFrame = true, fullStack = false, type } = options const logger = options.logger diff --git a/packages/vitest/src/node/reporters/github-actions.ts b/packages/vitest/src/node/reporters/github-actions.ts index ac751e45d6d8..7e65036dcd30 100644 --- a/packages/vitest/src/node/reporters/github-actions.ts +++ b/packages/vitest/src/node/reporters/github-actions.ts @@ -1,10 +1,8 @@ -import { Writable } from 'node:stream' import { getTasks } from '@vitest/runner/utils' import stripAnsi from 'strip-ansi' import type { File, Reporter, Vitest } from '../../types' import { getFullName } from '../../utils' -import { printError } from '../error' -import { Logger } from '../logger' +import { captuerPrintError } from '../error' import type { WorkspaceProject } from '../workspace' export class GithubActionsReporter implements Reporter { @@ -44,7 +42,7 @@ export class GithubActionsReporter implements Reporter { // format errors via `printError` for (const { project, title, error } of projectErrors) { - const result = await printErrorWrapper(error, this.ctx, project) + const result = await captuerPrintError(error, this.ctx, project) const stack = result?.nearest if (!stack) continue @@ -63,23 +61,6 @@ export class GithubActionsReporter implements Reporter { } } -// use Logger with custom Console to extract messgage from `processError` util -// TODO: maybe refactor `processError` to require single function `(message: string) => void` instead of full Logger? -export async function printErrorWrapper(error: unknown, ctx: Vitest, project: WorkspaceProject) { - let output = '' - const writable = new Writable({ - write(chunk, _encoding, callback) { - output += String(chunk) - callback() - }, - }) - const result = await printError(error, project, { - showCodeFrame: false, - logger: new Logger(ctx, writable, writable), - }) - return { nearest: result?.nearest, output } -} - // workflow command formatting based on // https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message // https://github.com/actions/toolkit/blob/f1d9b4b985e6f0f728b4b766db73498403fd5ca3/packages/core/src/command.ts#L80-L85 diff --git a/packages/vitest/src/node/reporters/junit.ts b/packages/vitest/src/node/reporters/junit.ts index f4df3b43b422..3054aba84b95 100644 --- a/packages/vitest/src/node/reporters/junit.ts +++ b/packages/vitest/src/node/reporters/junit.ts @@ -8,8 +8,8 @@ import stripAnsi from 'strip-ansi' import type { Vitest } from '../../node' import type { Reporter } from '../../types/reporter' import { getOutputFile } from '../../utils/config-helpers' +import { captuerPrintError } from '../error' import { IndentedLogger } from './renderers/indented-logger' -import { printErrorWrapper } from './github-actions' export interface JUnitOptions { outputFile?: string @@ -179,7 +179,7 @@ export class JUnitReporter implements Reporter { if (!error) return - const result = await printErrorWrapper( + const result = await captuerPrintError( error, this.ctx, this.ctx.getProjectByTaskId(task.id), From 3f9d54f2837dbb8c2f7e858c6a3469381b20b83b Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 27 Apr 2024 10:13:32 +0900 Subject: [PATCH 7/8] test: move test --- test/reporters/fixtures/error.test.ts | 3 +++ .../tests/__snapshots__/junit.test.ts.snap | 16 +++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/test/reporters/fixtures/error.test.ts b/test/reporters/fixtures/error.test.ts index 4a73c4d136b7..3b691c756cd5 100644 --- a/test/reporters/fixtures/error.test.ts +++ b/test/reporters/fixtures/error.test.ts @@ -32,6 +32,9 @@ it('number name object', () => { throw { name: 1234 }; }); +it('xml', () => { + throw new Error('error message that has XML in it
'); +}) function throwDeep() { throwSimple() diff --git a/test/reporters/tests/__snapshots__/junit.test.ts.snap b/test/reporters/tests/__snapshots__/junit.test.ts.snap index 1744f14468fe..7bcaeec8a22e 100644 --- a/test/reporters/tests/__snapshots__/junit.test.ts.snap +++ b/test/reporters/tests/__snapshots__/junit.test.ts.snap @@ -37,13 +37,13 @@ Error: afterAll error exports[`format error 1`] = ` " - - + + Error: throwSimple - ❯ throwSimple error.test.ts:41:9 - ❯ throwDeep error.test.ts:37:3 + ❯ throwSimple error.test.ts:44:9 + ❯ throwDeep error.test.ts:40:3 ❯ error.test.ts:8:3 @@ -97,10 +97,16 @@ Unknown Error: 1234 }
+ + +Error: error message that has XML in it <div><input/></div> + ❯ error.test.ts:36:9 + + Error: throwSuite - ❯ throwSuite error.test.ts:45:9 + ❯ throwSuite error.test.ts:48:9 ❯ error.test.ts:4:3 From 7d99cd2ae11bb9080d808aa2c058d13850f49064 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 27 Apr 2024 10:22:06 +0900 Subject: [PATCH 8/8] test: simplify old tests --- packages/vitest/src/node/error.ts | 6 +- test/reporters/src/data-for-junit.ts | 63 ------ .../__snapshots__/reporters.spec.ts.snap | 207 +----------------- test/reporters/tests/reporters.spec.ts | 103 ++------- 4 files changed, 33 insertions(+), 346 deletions(-) delete mode 100644 test/reporters/src/data-for-junit.ts diff --git a/packages/vitest/src/node/error.ts b/packages/vitest/src/node/error.ts index 6eee0ad8872d..91ca3ea9e80b 100644 --- a/packages/vitest/src/node/error.ts +++ b/packages/vitest/src/node/error.ts @@ -29,7 +29,11 @@ interface PrintErrorResult { } // use Logger with custom Console to capture entire error printing -export async function captuerPrintError(error: unknown, ctx: Vitest, project: WorkspaceProject) { +export async function captuerPrintError( + error: unknown, + ctx: Vitest, + project: WorkspaceProject, +) { let output = '' const writable = new Writable({ write(chunk, _encoding, callback) { diff --git a/test/reporters/src/data-for-junit.ts b/test/reporters/src/data-for-junit.ts deleted file mode 100644 index f443155fe558..000000000000 --- a/test/reporters/src/data-for-junit.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { AssertionError } from 'node:assert' -import type { File, Suite, Task } from 'vitest' - -function createSuiteHavingFailedTestWithXmlInError(): File[] { - const file: File = { - id: '1223128da3', - name: 'test/core/test/basic.test.ts', - type: 'suite', - meta: {}, - mode: 'run', - filepath: '/vitest/test/core/test/basic.test.ts', - result: { state: 'fail', duration: 145.99284195899963 }, - tasks: [], - projectName: '', - } - - const suite: Suite = { - id: '', - type: 'suite', - name: 'suite', - mode: 'run', - meta: {}, - file, - result: { state: 'pass', duration: 1.90183687210083 }, - tasks: [], - projectName: '', - } - - const errorWithXml = new AssertionError({ - message: 'error message that has XML in it ', - }) - - errorWithXml.stack = 'Error: error message that has XML in it \n' - + ' at /vitest/test/core/test/basic.test.ts:8:32\n' - + ' at /vitest/test/core/test/.ts:3:11\n' - + ' at etc....' - - const tasks: Task[] = [ - { - id: '123_0', - type: 'test', - name: 'test with xml in error', - mode: 'run', - meta: {}, - suite, - fails: undefined, - file, - result: { - state: 'fail', - errors: [errorWithXml], - duration: 2.123123123, - }, - context: null as any, - }, - ] - - file.tasks = [suite] - suite.tasks = tasks - - return [file] -} - -export { createSuiteHavingFailedTestWithXmlInError } diff --git a/test/reporters/tests/__snapshots__/reporters.spec.ts.snap b/test/reporters/tests/__snapshots__/reporters.spec.ts.snap index 34ffd5c7262d..b4e8c95b87ec 100644 --- a/test/reporters/tests/__snapshots__/reporters.spec.ts.snap +++ b/test/reporters/tests/__snapshots__/reporters.spec.ts.snap @@ -2,72 +2,14 @@ exports[`JUnit reporter (no outputFile entry) 1`] = ` " - - - - -AssertionError: expected 2.23606797749979 to equal 2 - ❯ test/core/test/basic.test.ts:8:32 - - - - - - - - - - - - - - - - - -[33merror[39m - - - - - - + " `; exports[`JUnit reporter 1`] = ` " - - - - -AssertionError: expected 2.23606797749979 to equal 2 - ❯ test/core/test/basic.test.ts:8:32 - - - - - - - - - - - - - - - - - -[33merror[39m - - - - - - + " `; @@ -79,36 +21,7 @@ exports[`JUnit reporter with outputFile 1`] = ` exports[`JUnit reporter with outputFile 2`] = ` " - - - - -AssertionError: expected 2.23606797749979 to equal 2 - ❯ test/core/test/basic.test.ts:8:32 - - - - - - - - - - - - - - - - - -[33merror[39m - - - - - - + " `; @@ -120,36 +33,7 @@ exports[`JUnit reporter with outputFile in non-existing directory 1`] = ` exports[`JUnit reporter with outputFile in non-existing directory 2`] = ` " - - - - -AssertionError: expected 2.23606797749979 to equal 2 - ❯ test/core/test/basic.test.ts:8:32 - - - - - - - - - - - - - - - - - -[33merror[39m - - - - - - + " `; @@ -161,36 +45,7 @@ exports[`JUnit reporter with outputFile object 1`] = ` exports[`JUnit reporter with outputFile object 2`] = ` " - - - - -AssertionError: expected 2.23606797749979 to equal 2 - ❯ test/core/test/basic.test.ts:8:32 - - - - - - - - - - - - - - - - - -[33merror[39m - - - - - - + " `; @@ -202,57 +57,7 @@ exports[`JUnit reporter with outputFile object in non-existing directory 1`] = ` exports[`JUnit reporter with outputFile object in non-existing directory 2`] = ` " - - - - -AssertionError: expected 2.23606797749979 to equal 2 - ❯ test/core/test/basic.test.ts:8:32 - - - - - - - - - - - - - - - - - -[33merror[39m - - - - - - - -" -`; - -exports[`JUnit reporter with outputFile with XML in error message 1`] = ` -"JUNIT report written to /report_escape_msg_xml.xml -" -`; - -exports[`JUnit reporter with outputFile with XML in error message 2`] = ` -" - - - - -AssertionError: error message that has XML in it <tag> - ❯ test/core/test/basic.test.ts:8:32 - ❯ test/core/test/<bracket-name>.ts:3:11 - - - + " `; diff --git a/test/reporters/tests/reporters.spec.ts b/test/reporters/tests/reporters.spec.ts index d98671193a0a..d9a7af45457c 100644 --- a/test/reporters/tests/reporters.spec.ts +++ b/test/reporters/tests/reporters.spec.ts @@ -1,5 +1,5 @@ import { existsSync, readFileSync, rmSync } from 'node:fs' -import { afterEach, expect, test, vi } from 'vitest' +import { beforeEach, expect, test, vi } from 'vitest' import { normalize, resolve } from 'pathe' import { JsonReporter } from '../../../packages/vitest/src/node/reporters/json' import { JUnitReporter } from '../../../packages/vitest/src/node/reporters/junit' @@ -7,10 +7,16 @@ import { TapReporter } from '../../../packages/vitest/src/node/reporters/tap' import { TapFlatReporter } from '../../../packages/vitest/src/node/reporters/tap-flat' import { getContext } from '../src/context' import { files } from '../src/data' -import { createSuiteHavingFailedTestWithXmlInError } from '../src/data-for-junit' -afterEach(() => { - vi.useRealTimers() +vi.mock('os', () => ({ + hostname: () => 'hostname', +})) + +beforeEach(() => { + vi.setSystemTime(1642587001759) + return () => { + vi.useRealTimers() + } }) test('tap reporter', async () => { @@ -39,90 +45,43 @@ test('tap-flat reporter', async () => { expect(context.output).toMatchSnapshot() }) -test.skip('JUnit reporter', async () => { +test('JUnit reporter', async () => { // Arrange const reporter = new JUnitReporter({}) const context = getContext() - vi.mock('os', () => ({ - hostname: () => 'hostname', - })) - - vi.setSystemTime(1642587001759) - // Act await reporter.onInit(context.vitest) - await reporter.onFinished(files) + await reporter.onFinished([]) // Assert expect(context.output).toMatchSnapshot() }) -test.skip('JUnit reporter (no outputFile entry)', async () => { +test('JUnit reporter (no outputFile entry)', async () => { // Arrange const reporter = new JUnitReporter({}) const context = getContext() context.vitest.config.outputFile = {} - vi.mock('os', () => ({ - hostname: () => 'hostname', - })) - - vi.setSystemTime(1642587001759) - // Act await reporter.onInit(context.vitest) - await reporter.onFinished(files) + await reporter.onFinished([]) // Assert expect(context.output).toMatchSnapshot() }) -test.skip('JUnit reporter with outputFile', async () => { +test('JUnit reporter with outputFile', async () => { // Arrange const reporter = new JUnitReporter({}) const outputFile = resolve('report.xml') const context = getContext() context.vitest.config.outputFile = outputFile - vi.mock('os', () => ({ - hostname: () => 'hostname', - })) - - vi.setSystemTime(1642587001759) - - // Act - await reporter.onInit(context.vitest) - await reporter.onFinished(files) - - // Assert - expect(normalizeCwd(context.output)).toMatchSnapshot() - expect(existsSync(outputFile)).toBe(true) - expect(readFileSync(outputFile, 'utf8')).toMatchSnapshot() - - // Cleanup - rmSync(outputFile) -}) - -test.skip('JUnit reporter with outputFile with XML in error message', async () => { - // Arrange - const reporter = new JUnitReporter({}) - const outputFile = resolve('report_escape_msg_xml.xml') - const context = getContext() - context.vitest.config.outputFile = outputFile - - vi.mock('os', () => ({ - hostname: () => 'hostname', - })) - - vi.setSystemTime(1642587001759) - - // setup suite with failed test with xml - const filesWithTestHavingXmlInError = createSuiteHavingFailedTestWithXmlInError() - // Act await reporter.onInit(context.vitest) - await reporter.onFinished(filesWithTestHavingXmlInError) + await reporter.onFinished([]) // Assert expect(normalizeCwd(context.output)).toMatchSnapshot() @@ -133,7 +92,7 @@ test.skip('JUnit reporter with outputFile with XML in error message', async () = rmSync(outputFile) }) -test.skip('JUnit reporter with outputFile object', async () => { +test('JUnit reporter with outputFile object', async () => { // Arrange const reporter = new JUnitReporter({}) const outputFile = resolve('report_object.xml') @@ -142,15 +101,9 @@ test.skip('JUnit reporter with outputFile object', async () => { junit: outputFile, } - vi.mock('os', () => ({ - hostname: () => 'hostname', - })) - - vi.setSystemTime(1642587001759) - // Act await reporter.onInit(context.vitest) - await reporter.onFinished(files) + await reporter.onFinished([]) // Assert expect(normalizeCwd(context.output)).toMatchSnapshot() @@ -161,7 +114,7 @@ test.skip('JUnit reporter with outputFile object', async () => { rmSync(outputFile) }) -test.skip('JUnit reporter with outputFile in non-existing directory', async () => { +test('JUnit reporter with outputFile in non-existing directory', async () => { // Arrange const reporter = new JUnitReporter({}) const rootDirectory = resolve('junitReportDirectory') @@ -169,15 +122,9 @@ test.skip('JUnit reporter with outputFile in non-existing directory', async () = const context = getContext() context.vitest.config.outputFile = outputFile - vi.mock('os', () => ({ - hostname: () => 'hostname', - })) - - vi.setSystemTime(1642587001759) - // Act await reporter.onInit(context.vitest) - await reporter.onFinished(files) + await reporter.onFinished([]) // Assert expect(normalizeCwd(context.output)).toMatchSnapshot() @@ -188,7 +135,7 @@ test.skip('JUnit reporter with outputFile in non-existing directory', async () = rmSync(outputFile) }) -test.skip('JUnit reporter with outputFile object in non-existing directory', async () => { +test('JUnit reporter with outputFile object in non-existing directory', async () => { // Arrange const reporter = new JUnitReporter({}) const rootDirectory = resolve('junitReportDirectory_object') @@ -198,15 +145,9 @@ test.skip('JUnit reporter with outputFile object in non-existing directory', asy junit: outputFile, } - vi.mock('os', () => ({ - hostname: () => 'hostname', - })) - - vi.setSystemTime(1642587001759) - // Act await reporter.onInit(context.vitest) - await reporter.onFinished(files) + await reporter.onFinished([]) // Assert expect(normalizeCwd(context.output)).toMatchSnapshot()