From 745a39a36d10807e08d9f845766b33b31f230ea1 Mon Sep 17 00:00:00 2001 From: Matthew Thomas Date: Sat, 3 Feb 2024 00:28:47 +0000 Subject: [PATCH] feat: add message and trace --- README.md | 2 ++ package.json | 2 +- src/generate-report.ts | 41 +++++++++++++++++++++++++++++++++-------- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a2e56b8..ca028af 100644 --- a/README.md +++ b/README.md @@ -137,3 +137,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 7fa6e2e..6d544e4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mocha-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 b52d2dc..b5fe9b2 100644 --- a/src/generate-report.ts +++ b/src/generate-report.ts @@ -1,5 +1,9 @@ import { type Runner, reporters } from 'mocha' -import { type CtrfEnvironment, type CtrfReport } from '../types/ctrf' +import { + type CtrfTest, + type CtrfEnvironment, + type CtrfReport, +} from '../types/ctrf' import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs' import { join } from 'path' @@ -106,16 +110,23 @@ class GenerateCtrfReport extends reporters.Base { } private updateCtrfTestResultsFromTest( - test: Mocha.Test, + testCase: Mocha.Test, ctrfReport: CtrfReport ): void { - const status = test.state ?? 'other' - - ctrfReport.results.tests.push({ - name: test.fullTitle(), + const status = testCase.state ?? 'other' + const test: CtrfTest = { + name: testCase.fullTitle(), status, - duration: test.duration ?? 0, - }) + duration: testCase.duration ?? 0, + } + + if (testCase.state === 'failed' && testCase.err != null) { + const failureDetails = this.extractFailureDetails(testCase) + test.message = failureDetails.message + test.trace = failureDetails.trace + } + + ctrfReport.results.tests.push(test) } private getReporterOptions(options: Options): ReporterOptions { @@ -204,6 +215,20 @@ class GenerateCtrfReport extends reporters.Base { return Object.keys(environment).length > 0 } + extractFailureDetails(testResult: Mocha.Test): Partial { + if (testResult.state === 'failed' && testResult.err !== undefined) { + const failureDetails: Partial = {} + if (testResult.err.message !== undefined) { + failureDetails.message = `${testResult.err.name} ${testResult.err.message}` + } + if (testResult.err.stack !== undefined) { + failureDetails.trace = testResult.err.stack + } + return failureDetails + } + return {} + } + private writeReportToFile(data: CtrfReport): void { const filePath = join( this.reporterOptions.outputDir ?? this.defaultOutputDir,