Skip to content

Commit

Permalink
fix: handling of AssertionResult.failureDetails
Browse files Browse the repository at this point in the history
Append AssertionResult.failureDetails to CtrfTest.trace when setting
.trace and .message attributes from the AssertionResult to prevent
overwriting .trace when AssertionResult.failureDetails is set.
  • Loading branch information
an-ky committed Jun 10, 2024
1 parent 616b2b8 commit ca7b9f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/generate-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class GenerateCtrfReport implements Reporter {
failureDetails.message = joinedMessages
.slice(0, match?.index)
.replace(colorCodesPattern, '')

Check failure on line 177 in src/generate-report.ts

View workflow job for this annotation

GitHub Actions / build

Delete `········`
failureDetails.trace = joinedMessages
.slice(match?.index)
.split('\n')
Expand All @@ -184,7 +185,7 @@ class GenerateCtrfReport implements Reporter {
}

if (testResult.failureDetails !== undefined) {
failureDetails.trace = testResult.failureMessages.join('\r\n')
failureDetails.trace = failureDetails.trace?.concat('\n\n', testResult.failureDetails.join('\n'))

Check failure on line 188 in src/generate-report.ts

View workflow job for this annotation

GitHub Actions / build

Replace `'\n\n',·testResult.failureDetails.join('\n')` with `⏎··········'\n\n',⏎··········testResult.failureDetails.join('\n')⏎········`
}
return failureDetails
}
Expand Down
26 changes: 25 additions & 1 deletion test/generate-report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ describe('GenerateDetailedCtrfReport', () => {
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',
],
Expand All @@ -251,4 +250,29 @@ describe('GenerateDetailedCtrfReport', () => {
'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'
)
})

it('should append failureDetails to trace', () => {
const mockTestCaseResult = {
status: 'failed' as Status,
fullName: 'Test Case Full Name',
ancestorTitles: ['parent'],
failureDetails: ['details'],
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\n\ndetails'
)
})
})

0 comments on commit ca7b9f3

Please sign in to comment.