Skip to content

Commit b3812e0

Browse files
authored
Merge pull request #664 from pespinel/feature/collapsed-option
2 parents 4a41472 + cd29956 commit b3812e0

File tree

5 files changed

+140
-12
lines changed

5 files changed

+140
-12
lines changed

__tests__/jest-junit.test.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,100 @@ describe('jest-junit tests', () => {
207207
// Report should have the title as the first line
208208
expect(report).toMatch(/^# My Custom Title\n/)
209209
})
210+
211+
it('report can be collapsed when configured', async () => {
212+
const fixturePath = path.join(__dirname, 'fixtures', 'jest-junit.xml')
213+
const filePath = normalizeFilePath(path.relative(__dirname, fixturePath))
214+
const fileContent = fs.readFileSync(fixturePath, {encoding: 'utf8'})
215+
216+
const opts: ParseOptions = {
217+
parseErrors: true,
218+
trackedFiles: []
219+
}
220+
221+
const parser = new JestJunitParser(opts)
222+
const result = await parser.parse(filePath, fileContent)
223+
const report = getReport([result], {
224+
...DEFAULT_OPTIONS,
225+
collapsed: 'always'
226+
})
227+
// Report should include collapsible details
228+
expect(report).toContain('<details><summary>Expand for details</summary>')
229+
expect(report).toContain('</details>')
230+
})
231+
232+
it('report is not collapsed when configured to never', async () => {
233+
const fixturePath = path.join(__dirname, 'fixtures', 'jest-junit.xml')
234+
const filePath = normalizeFilePath(path.relative(__dirname, fixturePath))
235+
const fileContent = fs.readFileSync(fixturePath, {encoding: 'utf8'})
236+
237+
const opts: ParseOptions = {
238+
parseErrors: true,
239+
trackedFiles: []
240+
}
241+
242+
const parser = new JestJunitParser(opts)
243+
const result = await parser.parse(filePath, fileContent)
244+
const report = getReport([result], {
245+
...DEFAULT_OPTIONS,
246+
collapsed: 'never'
247+
})
248+
// Report should not include collapsible details
249+
expect(report).not.toContain('<details><summary>Expand for details</summary>')
250+
expect(report).not.toContain('</details>')
251+
})
252+
253+
it('report auto-collapses when all tests pass', async () => {
254+
// Test with a fixture that has all passing tests (no failures)
255+
const fixturePath = path.join(__dirname, 'fixtures', 'jest-junit-eslint.xml')
256+
const filePath = normalizeFilePath(path.relative(__dirname, fixturePath))
257+
const fileContent = fs.readFileSync(fixturePath, {encoding: 'utf8'})
258+
259+
const opts: ParseOptions = {
260+
parseErrors: true,
261+
trackedFiles: []
262+
}
263+
264+
const parser = new JestJunitParser(opts)
265+
const result = await parser.parse(filePath, fileContent)
266+
267+
// Verify this fixture has no failures
268+
expect(result.failed).toBe(0)
269+
270+
const report = getReport([result], {
271+
...DEFAULT_OPTIONS,
272+
collapsed: 'auto'
273+
})
274+
275+
// Should collapse when all tests pass
276+
expect(report).toContain('<details><summary>Expand for details</summary>')
277+
expect(report).toContain('</details>')
278+
})
279+
280+
it('report does not auto-collapse when tests fail', async () => {
281+
// Test with a fixture that has failing tests
282+
const fixturePath = path.join(__dirname, 'fixtures', 'jest-junit.xml')
283+
const filePath = normalizeFilePath(path.relative(__dirname, fixturePath))
284+
const fileContent = fs.readFileSync(fixturePath, {encoding: 'utf8'})
285+
286+
const opts: ParseOptions = {
287+
parseErrors: true,
288+
trackedFiles: []
289+
}
290+
291+
const parser = new JestJunitParser(opts)
292+
const result = await parser.parse(filePath, fileContent)
293+
294+
// Verify this fixture has failures
295+
expect(result.failed).toBeGreaterThan(0)
296+
297+
const report = getReport([result], {
298+
...DEFAULT_OPTIONS,
299+
collapsed: 'auto'
300+
})
301+
302+
// Should not collapse when there are failures
303+
expect(report).not.toContain('<details><summary>Expand for details</summary>')
304+
expect(report).not.toContain('</details>')
305+
})
210306
})

action.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ inputs:
8989
description: Customize badge title
9090
required: false
9191
default: 'tests'
92+
collapsed:
93+
description: |
94+
Controls whether test report details are collapsed or expanded. Supported options:
95+
- auto: Collapse only if all tests pass (default behavior)
96+
- always: Always collapse the report details
97+
- never: Always expand the report details
98+
required: false
99+
default: 'auto'
92100
token:
93101
description: GitHub Access Token
94102
required: false

dist/index.js

Lines changed: 16 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class TestReporter {
4949
readonly useActionsSummary = core.getInput('use-actions-summary', {required: false}) === 'true'
5050
readonly badgeTitle = core.getInput('badge-title', {required: false})
5151
readonly reportTitle = core.getInput('report-title', {required: false})
52+
readonly collapsed = core.getInput('collapsed', {required: false}) as 'auto' | 'always' | 'never'
5253
readonly token = core.getInput('token', {required: true})
5354
readonly octokit: InstanceType<typeof GitHub>
5455
readonly context = getCheckRunContext()
@@ -66,6 +67,11 @@ class TestReporter {
6667
return
6768
}
6869

70+
if (this.collapsed !== 'auto' && this.collapsed !== 'always' && this.collapsed !== 'never') {
71+
core.setFailed(`Input parameter 'collapsed' has invalid value`)
72+
return
73+
}
74+
6975
if (isNaN(this.maxAnnotations) || this.maxAnnotations < 0 || this.maxAnnotations > 50) {
7076
core.setFailed(`Input parameter 'max-annotations' has invalid value`)
7177
return
@@ -166,7 +172,7 @@ class TestReporter {
166172
}
167173
}
168174

169-
const {listSuites, listTests, onlySummary, useActionsSummary, badgeTitle, reportTitle} = this
175+
const {listSuites, listTests, onlySummary, useActionsSummary, badgeTitle, reportTitle, collapsed} = this
170176

171177
const passed = results.reduce((sum, tr) => sum + tr.passed, 0)
172178
const failed = results.reduce((sum, tr) => sum + tr.failed, 0)
@@ -182,7 +188,8 @@ class TestReporter {
182188
onlySummary,
183189
useActionsSummary,
184190
badgeTitle,
185-
reportTitle
191+
reportTitle,
192+
collapsed
186193
})
187194

188195
core.info('Summary content:')
@@ -211,7 +218,8 @@ class TestReporter {
211218
onlySummary,
212219
useActionsSummary,
213220
badgeTitle,
214-
reportTitle
221+
reportTitle,
222+
collapsed
215223
})
216224

217225
core.info('Creating annotations')

src/report/get-report.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface ReportOptions {
1616
useActionsSummary: boolean
1717
badgeTitle: string
1818
reportTitle: string
19+
collapsed: 'auto' | 'always' | 'never'
1920
}
2021

2122
export const DEFAULT_OPTIONS: ReportOptions = {
@@ -25,7 +26,8 @@ export const DEFAULT_OPTIONS: ReportOptions = {
2526
onlySummary: false,
2627
useActionsSummary: true,
2728
badgeTitle: 'tests',
28-
reportTitle: ''
29+
reportTitle: '',
30+
collapsed: 'auto'
2931
}
3032

3133
export function getReport(results: TestRunResult[], options: ReportOptions = DEFAULT_OPTIONS): string {
@@ -154,7 +156,11 @@ export function getBadge(passed: number, failed: number, skipped: number, option
154156
function getTestRunsReport(testRuns: TestRunResult[], options: ReportOptions): string[] {
155157
const sections: string[] = []
156158
const totalFailed = testRuns.reduce((sum, tr) => sum + tr.failed, 0)
157-
if (totalFailed === 0) {
159+
160+
// Determine if report should be collapsed based on collapsed option
161+
const shouldCollapse = options.collapsed === 'always' || (options.collapsed === 'auto' && totalFailed === 0)
162+
163+
if (shouldCollapse) {
158164
sections.push(`<details><summary>Expand for details</summary>`)
159165
sections.push(` `)
160166
}
@@ -187,7 +193,7 @@ function getTestRunsReport(testRuns: TestRunResult[], options: ReportOptions): s
187193
sections.push(...suitesReports)
188194
}
189195

190-
if (totalFailed === 0) {
196+
if (shouldCollapse) {
191197
sections.push(`</details>`)
192198
}
193199
return sections

0 commit comments

Comments
 (0)