Skip to content

Commit

Permalink
Merge pull request #2 from ctrf-io/feat/message-trace
Browse files Browse the repository at this point in the history
feat: add message and trace
  • Loading branch information
Ma11hewThomas authored Feb 3, 2024
2 parents f57585e + 745a39a commit 5b0274b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mocha-ctrf-json-reporter",
"version": "0.0.2",
"version": "0.0.3",
"description": "",
"main": "dist/index.js",
"scripts": {
Expand Down
41 changes: 33 additions & 8 deletions src/generate-report.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -204,6 +215,20 @@ class GenerateCtrfReport extends reporters.Base {
return Object.keys(environment).length > 0
}

extractFailureDetails(testResult: Mocha.Test): Partial<CtrfTest> {
if (testResult.state === 'failed' && testResult.err !== undefined) {
const failureDetails: Partial<CtrfTest> = {}
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,
Expand Down

0 comments on commit 5b0274b

Please sign in to comment.