Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vitest): correctly report failed test files as failures in json reporter, export json reporter types #5081

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/vitest/src/node/reporters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export {
}
export type { BaseReporter, Reporter }

export type { JsonAssertionResult, JsonTestResult, JsonTestResults } from './json'

export const ReportersMap = {
'default': DefaultReporter,
'basic': BasicReporter,
Expand Down
21 changes: 11 additions & 10 deletions packages/vitest/src/node/reporters/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const StatusMap: Record<TaskState, Status> = {
todo: 'todo',
}

interface FormattedAssertionResult {
export interface JsonAssertionResult {
ancestorTitles: Array<string>
fullName: string
status: Status
Expand All @@ -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<FormattedAssertionResult>
assertionResults: Array<JsonAssertionResult>
// summary: string
// coverage: unknown
}

interface FormattedTestResults {
export interface JsonTestResults {
numFailedTests: number
numFailedTestSuites: number
numPassedTests: number
Expand All @@ -55,7 +55,7 @@ interface FormattedTestResults {
numTotalTestSuites: number
startTime: number
success: boolean
testResults: Array<FormattedTestResult>
testResults: Array<JsonTestResult>
// coverageMap?: CoverageMap | null | undefined
// numRuntimeErrorTestSuites: number
// snapshot: SnapshotSummary
Expand Down Expand Up @@ -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<FormattedTestResult> = []
const testResults: Array<JsonTestResult> = []

const success = numFailedTestSuites === 0 && numFailedTests === 0

Expand Down Expand Up @@ -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')) {
Expand All @@ -120,20 +120,21 @@ 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 ?? '',
name: file.filepath,
})
}

const result: FormattedTestResults = {
const result: JsonTestResults = {
numTotalTestSuites,
numPassedTestSuites,
numFailedTestSuites,
Expand Down
1 change: 1 addition & 0 deletions test/reporters/fixtures/json-fail-import.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('failed test suite')
2 changes: 1 addition & 1 deletion test/reporters/tests/html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
14 changes: 11 additions & 3 deletions test/reporters/tests/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down