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

feat: add failure message #9

Merged
merged 1 commit into from
Feb 22, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,5 @@ The test object in the report includes the following [CTRF properties](https://c
| `name` | String | Required | The name of the test. |
| `status` | String | Required | The outcome of the test. One of: `passed`, `failed`, `skipped`, `pending`, `other`. |
| `duration` | Number | Required | The time taken for the test execution, in milliseconds. |
| `message` | String | Optional | The failure message if the test failed. |
| `trace` | String | Optional | The stack trace captured if the test failed. |
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-ctrf-json-reporter",
"version": "0.0.2",
"version": "0.0.3",
"description": "",
"main": "dist/index.js",
"scripts": {
Expand Down
34 changes: 31 additions & 3 deletions src/generate-report.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { type TestResult, type Test, type Status } from '@jest/test-result'
import {
type TestResult,
type Test,
type Status,
type AssertionResult,
} from '@jest/test-result'
import { type Reporter, type ReporterContext } from '@jest/reporters'
import { type Config } from '@jest/types'
import {
type CtrfReport,
type CtrfTestState,
type CtrfEnvironment,
type CtrfTest,
} from '../types/ctrf'

import * as fs from 'fs'
Expand Down Expand Up @@ -115,14 +121,36 @@ class GenerateCtrfReport implements Reporter {

private updateCtrfTestResultsFromTestResult(testResult: TestResult): void {
testResult.testResults.forEach((testCaseResult) => {
this.ctrfReport.results.tests.push({
const test: CtrfTest = {
name: testCaseResult.fullName,
duration: testCaseResult.duration ?? 0,
status: this.mapStatus(testCaseResult.status),
})
}

test.message = this.extractFailureDetails(testCaseResult).message
test.trace = this.extractFailureDetails(testCaseResult).trace

this.ctrfReport.results.tests.push(test)
})
}

extractFailureDetails(testResult: AssertionResult): Partial<CtrfTest> {
if (
testResult.status === 'failed' &&
testResult.failureMessages !== undefined
) {
const failureDetails: Partial<CtrfTest> = {}
if (testResult.failureMessages !== undefined) {
failureDetails.message = testResult.failureMessages.join('\r\n')
}
if (testResult.failureDetails !== undefined) {
failureDetails.trace = testResult.failureMessages.join('\r\n')
}
return failureDetails
}
return {}
}

private updateTotalsFromTestResult(testResult: TestResult): void {
testResult.testResults.forEach((testCaseResult) => {
const ctrfStatus = this.mapStatus(testCaseResult.status)
Expand Down
Loading