Skip to content

Commit

Permalink
Merge pull request #12 from an-ky/feat--separate-message-from-stack-t…
Browse files Browse the repository at this point in the history
…race

Feat  separate message from stack trace
  • Loading branch information
Ma11hewThomas authored Jun 7, 2024
2 parents 8f547b0 + 814fb7b commit 736a66c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ The test object in the report includes the following [CTRF properties](https://c
| `rawStatus` | String | Optional | The original jest status of the test before mapping to CTRF status. |
| `type` | String | Optional | The type of test (e.g., `unit`, `component`). |
| `filepath` | String | Optional | The file path where the test is located in the project. |
| `retries` | Number | Optional | The number of retries attempted for the test. |
| `retries` | Number | Optional | The number of retries attempted for the test. |
| `flaky` | Boolean | Optional | Indicates whether the test result is flaky. |

## Support Us
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion src/generate-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class GenerateCtrfReport implements Reporter {
readonly reporterName = 'jest-ctrf-json-reporter'
readonly defaultOutputFile = 'ctrf-report.json'
readonly defaultOutputDir = 'ctrf'

filename = this.defaultOutputFile

constructor(
Expand Down Expand Up @@ -148,14 +149,30 @@ class GenerateCtrfReport implements Reporter {
}

extractFailureDetails(testResult: AssertionResult): Partial<CtrfTest> {
const messageStackTracePattern = /^\s{4}at/mu
// eslint-disable-next-line no-control-regex
const colorCodesPattern = /\x1b\[\d+m/gmu

if (
testResult.status === 'failed' &&
testResult.failureMessages !== undefined
) {
const failureDetails: Partial<CtrfTest> = {}
if (testResult.failureMessages !== undefined) {
failureDetails.message = testResult.failureMessages.join('\r\n')
const joinedMessages = testResult.failureMessages.join('\n')
const match = joinedMessages.match(messageStackTracePattern)
failureDetails.message = joinedMessages
.slice(0, match?.index)
.replace(colorCodesPattern, '')
failureDetails.trace = joinedMessages
.slice(match?.index)
.split('\n')
.map((line) => {
return line.trim()
})
.join('\n')
}

if (testResult.failureDetails !== undefined) {
failureDetails.trace = testResult.failureMessages.join('\r\n')
}
Expand Down
66 changes: 66 additions & 0 deletions test/generate-report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,69 @@ describe('GenerateCtrfReport', () => {
)
})
})

describe('GenerateDetailedCtrfReport', () => {
let reporter: GenerateCtrfReport
beforeEach(() => {
const mockGlobalConfig: Config.GlobalConfig = {} as Config.GlobalConfig
const mockreporterOptions: ReporterConfigOptions = {
minimal: false,
} as ReporterConfigOptions
const mockreporterContext: ReporterContext = {} as ReporterContext
reporter = new GenerateCtrfReport(
mockGlobalConfig,
mockreporterOptions,
mockreporterContext
)
})

it('should update the ctrfReport message with error description from failureMessages on failed builds', () => {
const mockTestCaseResult = {
status: 'failed' as Status,
fullName: 'Test Case Full Name',
ancestorTitles: ['parent'],
duration: 100,

failureMessages: [
'Error: \u001b[2mexpect(\u001b[22m\u001b[31mreceived\u001b[39m\u001b[2m).\u001b[22mtoBe\u001b[2m(\u001b[22m\u001b[32mexpected\u001b[39m\u001b[2m) // Object.is equality\u001b[22m\n\nExpected: \u001b[32m"b"\u001b[39m\nReceived: \u001b[31mundefined\u001b[39m\n at Object.<anonymous> (/jest-ctrf-json-reporter/test/generate-report.test.ts:133:41)\n at Promise.then.completed (/jest-ctrf-json-reporter/node_modules/jest-circus/build/utils.js:298:28)\n)',
],
}
const mockResult: TestResult = {
testFilePath: '/path/to/test.ts',
testResults: [mockTestCaseResult],
} as TestResult

;(reporter as any).updateCtrfTestResultsFromTestResult(mockResult)

const updatedTestResult = reporter['ctrfReport'].results.tests[0]

expect(updatedTestResult.message).toBe(
'Error: expect(received).toBe(expected) // Object.is equality\n\nExpected: "b"\nReceived: undefined\n'
)
})

it('should update the ctrfReport trace with stack trace from failureMessage on failed builds', () => {
const mockTestCaseResult = {
status: 'failed' as Status,
fullName: 'Test Case Full Name',
ancestorTitles: ['parent'],
duration: 100,

failureMessages: [
'Error: \u001b[2mexpect(\u001b[22m\u001b[31mreceived\u001b[39m\u001b[2m).\u001b[22mtoBe\u001b[2m(\u001b[22m\u001b[32mexpected\u001b[39m\u001b[2m) // Object.is equality\u001b[22m\n\nExpected: \u001b[32m"b"\u001b[39m\nReceived: \u001b[31mundefined\u001b[39m\n at Object.<anonymous> (/jest-ctrf-json-reporter/test/generate-report.test.ts:133:41)\n at Promise.then.completed (/jest-ctrf-json-reporter/node_modules/jest-circus/build/utils.js:298:28)\n',
],
}
const mockResult: TestResult = {
testFilePath: '/path/to/test.ts',
testResults: [mockTestCaseResult],
} as TestResult

;(reporter as any).updateCtrfTestResultsFromTestResult(mockResult)

const updatedTestResult = reporter['ctrfReport'].results.tests[0]

expect(updatedTestResult.trace).toBe(
'at Object.<anonymous> (/jest-ctrf-json-reporter/test/generate-report.test.ts:133:41)\nat Promise.then.completed (/jest-ctrf-json-reporter/node_modules/jest-circus/build/utils.js:298:28)\n'
)
})
})

0 comments on commit 736a66c

Please sign in to comment.