Skip to content

Commit

Permalink
Support multiple junitxml files (#35)
Browse files Browse the repository at this point in the history
* add `multiple-junitxml-files` to readme

* add `withoutHeader` to junit report

* move `parseLine` to utils

* add full multiple junit support

* add tests for multiple junit files

* build

* 1.0.18

* update readme with screenshot for multiple junit
  • Loading branch information
MishaKav authored Oct 15, 2022
1 parent ebc4ddf commit d6b4f5e
Show file tree
Hide file tree
Showing 17 changed files with 345 additions and 100 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ You can add this action to your GitHub workflow for Ubuntu runners (e.g. `runs-o
| `coverage-path-prefix` | | '' | Prefix for path when link to files in comment |
| `report-only-changed-files` | | false | Show in report only changed files for this commit, and not all files |
| `multiple-files` | | '' | You can pass array of `json-summary.json` files and generate single comment with table of results<br/>Single line should look like `Title1, ./path/to/json-sumamry.json` |
| `multiple-junitxml-files` | | '' | You can pass array of `junit.xml` files and generate single comment with table of results<br/>Single line should look like `Title1, ./path/to/junit.xml` |

## Output Variables

Expand Down Expand Up @@ -260,6 +261,20 @@ Generated from `junit.xml` by [jest-junit](https://www.npmjs.com/package/jest-ju
junitxml-path: ./junit.xml
```

Example GitHub Action workflow that uses multiple junit files mode (can be useful on mono-repo projects)

![image](https://user-images.githubusercontent.com/289035/195997703-95d331a3-beba-4567-831e-22d1f0e977da.png)

```yaml
- name: Jest coverage comment
uses: MishaKav/jest-coverage-comment@main
with:
multiple-junitxml-files: |
My-Title-1, ./coverage_1/junit.xml
My-Title-2, ./coverage_2/junit.xml
```


Example GitHub Action workflow that will update your `README.md` with coverage summary, only on merge to `main` branch
All you need is to add in your `README.md` the following lines wherever you want.
If your coverage summary report will not change, it wouldn't push any changes to readme file.
Expand Down
4 changes: 1 addition & 3 deletions __tests__/junit.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as core from '@actions/core'
import { expect, test, describe, jest } from '@jest/globals'
import { getJunitReport, exportedForTesting } from '../src/junit'

const { parseJunit, junitToMarkdown } = exportedForTesting
import { getJunitReport, parseJunit, junitToMarkdown } from '../src/junit'

describe('parsing junit', () => {
test('should parse xml string to junit', async () => {
Expand Down
27 changes: 1 addition & 26 deletions __tests__/multi-files.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
import * as core from '@actions/core'
import { expect, test, describe, jest } from '@jest/globals'
import { getMultipleReport, exportedForTesting } from '../src/multi-files'

const { parseLine } = exportedForTesting

describe('parsing one-line', () => {
test('should not parse bad line', () => {
const parsedLine1 = parseLine('')
const parsedLine2 = parseLine('some bad line')
const parsedLine3 = parseLine(`title only`)
const parsedLine4 = parseLine(`./path/to/file.json`)

expect(parsedLine1).toBeNull()
expect(parsedLine2).toBeNull()
expect(parsedLine3).toBeNull()
expect(parsedLine4).toBeNull()
})

test('should parse correctly one-line', async () => {
const parsedLine1 = parseLine(`title1, ./path/to/file.json`)
const parsedLine2 = parseLine(`title1,./path/to/file.json`)
const expectedResult = { title: 'title1', file: './path/to/file.json' }

expect(parsedLine1).toMatchObject(expectedResult)
expect(parsedLine2).toMatchObject(expectedResult)
})
})
import { getMultipleReport } from '../src/multi-files'

describe('multi report', () => {
test('should not parse when no files', () => {
Expand Down
72 changes: 72 additions & 0 deletions __tests__/multi-junit-files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as core from '@actions/core'
import { expect, test, describe, jest } from '@jest/globals'
import { getMultipleJunitReport } from '../src/multi-junit-files'

describe('multi junit report', () => {
test('should not parse when no files', async () => {
// @ts-ignore
const result1 = await getMultipleJunitReport({ multipleJunitFiles: [] })
// @ts-ignore
const result2 = await getMultipleJunitReport({ multipleJunitFiles: [' '] })

expect(result1).toBeNull()
expect(result2).toBeNull()
})

test('should throw error on bad format', async () => {
const spy = jest.spyOn(core, 'error')
// @ts-ignore
const result = await getMultipleJunitReport({
multipleJunitFiles: ['./path/to/file.xml'],
})

expect(result).toBeNull()
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith(
`Generating report for multiple junit files. No files are provided`
)
})

test('should throw warning when file not exist', async () => {
const spy = jest.spyOn(core, 'warning')
// @ts-ignore
const result = await getMultipleJunitReport({
multipleJunitFiles: ['title1, ./path/to/junit.xml'],
})

expect(result).toBeNull()
expect(spy).toHaveBeenCalledTimes(2)
expect(spy).toHaveBeenCalledWith(`File "./path/to/junit.xml" doesn't exist`)
expect(spy).toHaveBeenCalledWith(`Junit xml was not provided`)
})

test('should generate markdown for one file', async () => {
const html = `| Title | Tests | Skipped | Failures | Errors | Time |
| ----- | ----- | ------- | -------- | -------- | ------------------ |
| title1 | 6 | 0 :zzz: | 0 :x: | 0 :fire: | 0.732s :stopwatch: |
`
// @ts-ignore
const result = await getMultipleJunitReport({
multipleJunitFiles: [`title1, ${__dirname}/../data/coverage_1/junit.xml`],
})

expect(result).toBe(html)
})

test('should generate markdown for two files', async () => {
const html = `| Title | Tests | Skipped | Failures | Errors | Time |
| ----- | ----- | ------- | -------- | -------- | ------------------ |
| title1 | 6 | 0 :zzz: | 0 :x: | 0 :fire: | 0.732s :stopwatch: |
| title2 | 6 | 0 :zzz: | 0 :x: | 0 :fire: | 0.732s :stopwatch: |
`
// @ts-ignore
const result = await getMultipleJunitReport({
multipleJunitFiles: [
`title1, ${__dirname}/../data/coverage_1/junit.xml`,
`title2, ${__dirname}/../data/coverage_1/junit.xml`,
],
})

expect(result).toBe(html)
})
})
30 changes: 29 additions & 1 deletion __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {
beforeAll,
afterAll,
} from '@jest/globals'
import { getContentFile, getCoverageColor, getPathToFile } from '../src/utils'
import {
getContentFile,
getCoverageColor,
getPathToFile,
parseLine,
} from '../src/utils'

describe('should check all utils functions', () => {
const GITHUB_WORKSPACE = process.cwd()
Expand Down Expand Up @@ -112,4 +117,27 @@ describe('should check all utils functions', () => {
expect(getCoverageColor(-1)).toBe('red')
})
})

describe('parsing one-line', () => {
test('should not parse bad line', () => {
const parsedLine1 = parseLine('')
const parsedLine2 = parseLine('some bad line')
const parsedLine3 = parseLine(`title only`)
const parsedLine4 = parseLine(`./path/to/file.json`)

expect(parsedLine1).toBeNull()
expect(parsedLine2).toBeNull()
expect(parsedLine3).toBeNull()
expect(parsedLine4).toBeNull()
})

test('should parse correctly one-line', async () => {
const parsedLine1 = parseLine(`title1, ./path/to/file.json`)
const parsedLine2 = parseLine(`title1,./path/to/file.json`)
const expectedResult = { title: 'title1', file: './path/to/file.json' }

expect(parsedLine1).toMatchObject(expectedResult)
expect(parsedLine2).toMatchObject(expectedResult)
})
})
})
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ inputs:
default: ''
required: false

multiple-junitxml-files:
description: >
You can pass array of `junit.xml` files and generate single comment with table of results
Single line should look like `Title1, ./path/to/junit.xml`
default: ''
required: false

outputs:
coverage:
description: 'Value indicating the coverage percentage of your report based on jest, example 78'
Expand Down
Loading

0 comments on commit d6b4f5e

Please sign in to comment.