diff --git a/packages/vitest/src/node/reporters/index.ts b/packages/vitest/src/node/reporters/index.ts index 0bece51d7db2..7fc76117b712 100644 --- a/packages/vitest/src/node/reporters/index.ts +++ b/packages/vitest/src/node/reporters/index.ts @@ -23,6 +23,8 @@ export { } export type { BaseReporter, Reporter } +export type { JsonAssertionResult, JsonTestResult, JsonTestResults } from './json' + export const ReportersMap = { 'default': DefaultReporter, 'basic': BasicReporter, diff --git a/packages/vitest/src/node/reporters/json.ts b/packages/vitest/src/node/reporters/json.ts index 0f59b765abf5..019c9405b79b 100644 --- a/packages/vitest/src/node/reporters/json.ts +++ b/packages/vitest/src/node/reporters/json.ts @@ -22,7 +22,7 @@ const StatusMap: Record = { todo: 'todo', } -interface FormattedAssertionResult { +export interface JsonAssertionResult { ancestorTitles: Array fullName: string status: Status @@ -32,18 +32,18 @@ interface FormattedAssertionResult { location?: Callsite | null } -interface FormattedTestResult { +export interface JsonTestResult { message: string name: string status: 'failed' | 'passed' startTime: number endTime: number - assertionResults: Array + assertionResults: Array // summary: string // coverage: unknown } -interface FormattedTestResults { +export interface JsonTestResults { numFailedTests: number numFailedTestSuites: number numPassedTests: number @@ -55,7 +55,7 @@ interface FormattedTestResults { numTotalTestSuites: number startTime: number success: boolean - testResults: Array + testResults: Array // coverageMap?: CoverageMap | null | undefined // numRuntimeErrorTestSuites: number // snapshot: SnapshotSummary @@ -83,7 +83,7 @@ export class JsonReporter implements Reporter { const numPassedTests = numTotalTests - numFailedTests const numPendingTests = tests.filter(t => t.result?.state === 'run').length const numTodoTests = tests.filter(t => t.mode === 'todo').length - const testResults: Array = [] + const testResults: Array = [] const success = numFailedTestSuites === 0 && numFailedTests === 0 @@ -111,7 +111,7 @@ export class JsonReporter implements Reporter { duration: t.result?.duration, failureMessages: t.result?.errors?.map(e => e.message) || [], location: await this.getFailureLocation(t), - } as FormattedAssertionResult + } as JsonAssertionResult })) if (tests.some(t => t.result?.state === 'run')) { @@ -120,12 +120,13 @@ export class JsonReporter implements Reporter { + 'Please report it to https://github.com/vitest-dev/vitest/issues') } + const hasFailedTests = tests.some(t => t.result?.state === 'fail') + testResults.push({ assertionResults, startTime, endTime, - status: tests.some(t => - t.result?.state === 'fail') + status: file.result?.state === 'fail' || hasFailedTests ? 'failed' : 'passed', message: file.result?.errors?.[0]?.message ?? '', @@ -133,7 +134,7 @@ export class JsonReporter implements Reporter { }) } - const result: FormattedTestResults = { + const result: JsonTestResults = { numTotalTestSuites, numPassedTestSuites, numFailedTestSuites, diff --git a/test/reporters/fixtures/json-fail-import.test.ts b/test/reporters/fixtures/json-fail-import.test.ts new file mode 100644 index 000000000000..e6462ca7266e --- /dev/null +++ b/test/reporters/fixtures/json-fail-import.test.ts @@ -0,0 +1 @@ +throw new Error('failed test suite') diff --git a/test/reporters/tests/html.test.ts b/test/reporters/tests/html.test.ts index 6a7269b4d5ad..705be1fb4154 100644 --- a/test/reporters/tests/html.test.ts +++ b/test/reporters/tests/html.test.ts @@ -40,7 +40,7 @@ describe('html reporter', async () => { }, 120000) it('resolves to "failing" status for test file "json-fail"', async () => { - const [expected, testFile, basePath] = ['failing', 'json-fail', 'html/fail'] + const [expected, testFile, basePath] = ['failing', 'json-fail.test', 'html/fail'] await runVitest({ reporters: 'html', outputFile: `${basePath}/index.html`, root }, [testFile]) diff --git a/test/reporters/tests/json.test.ts b/test/reporters/tests/json.test.ts index e6bdcc820cbc..7edd5319e0c9 100644 --- a/test/reporters/tests/json.test.ts +++ b/test/reporters/tests/json.test.ts @@ -11,10 +11,18 @@ describe('json reporter', async () => { const data = JSON.parse(stdout) - expect(data.testResults).toHaveLength(1) - expect(data.testResults[0].assertionResults).toHaveLength(1) + expect(data.testResults).toHaveLength(2) + + const failedImport = data.testResults.find((r: any) => r.name.includes('json-fail-import.test'))! + const failedTest = data.testResults.find((r: any) => r.name.includes('json-fail.test'))! + + expect(failedTest.assertionResults).toHaveLength(1) + expect(failedImport.assertionResults).toHaveLength(0) + + expect(failedTest.status).toBe('failed') + expect(failedImport.status).toBe('failed') - const result = data.testResults[0].assertionResults[0] + const result = failedTest.assertionResults[0] delete result.duration expect(result).toMatchSnapshot() }, 40000)