diff --git a/README.md b/README.md index e441ef2..a794300 100644 --- a/README.md +++ b/README.md @@ -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. | diff --git a/package.json b/package.json index d56ce64..90c6585 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jest-ctrf-json-reporter", - "version": "0.0.2", + "version": "0.0.3", "description": "", "main": "dist/index.js", "scripts": { diff --git a/src/generate-report.ts b/src/generate-report.ts index 44c4571..8c1904f 100644 --- a/src/generate-report.ts +++ b/src/generate-report.ts @@ -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' @@ -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 { + if ( + testResult.status === 'failed' && + testResult.failureMessages !== undefined + ) { + const failureDetails: Partial = {} + 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)