Skip to content

Commit

Permalink
Merge pull request #9 from ctrf-io/feat/failure-messages
Browse files Browse the repository at this point in the history
feat: add failure message
  • Loading branch information
Ma11hewThomas authored Feb 22, 2024
2 parents 02e769d + c74c024 commit c0c8e6c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
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

0 comments on commit c0c8e6c

Please sign in to comment.