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()