Skip to content

Commit

Permalink
feat: save coverage report to file
Browse files Browse the repository at this point in the history
zivaja committed Nov 24, 2023
1 parent a1f1f6c commit 723894d
Showing 5 changed files with 42 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ jobs:
name: "Unit tests"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
- run: npm ci
- run: npm test

@@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
- run: npm ci
- uses: ./
env:
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Forked from [mattallty/jest-github-action](https://github.com/mattallty/jest-github-action).
I want to sincerely thank [mattallty](https://github.com/mattallty) for their initial work on the Jest Github Action. Their effort laid the groundwork for what we have achieved, and I am truly grateful for that.

Using their repository as a starting point has enabled us to enhance the action with new features. I appreciate the opportunity to build upon their work and improve it further.

A big thank you goes to [mattallty](https://github.com/mattallty) for their invaluable contribution. Their original work has been essential to our project's development.

# Jest Github Action

Main features:
@@ -18,7 +25,7 @@ Main features:
You can now consume the action by referencing the v1 branch

```yaml
uses: mattallty/jest-github-action@v1
uses: EkoCommunications/jest-github-action@v1.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
@@ -29,7 +36,7 @@ By default, this action will execute `npm test` to run your tests.
You can change this behavior by providing a custom `test-command` like this:

```yaml
uses: mattallty/jest-github-action@v1
uses: EkoCommunications/jest-github-action@v1.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
@@ -40,7 +47,7 @@ with:
### Running tests only on changed files

```yaml
uses: mattallty/jest-github-action@v1
uses: EkoCommunications/jest-github-action@v1.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
@@ -52,7 +59,7 @@ with:
### Silencing the code coverage comment

```yaml
uses: mattallty/jest-github-action@v1
uses: EkoCommunications/jest-github-action@v1.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
@@ -65,12 +72,13 @@ with:
For running tests in folders other than root, supply a working-directory.

```yaml
uses: mattallty/jest-github-action@v1
uses: EkoCommunications/jest-github-action@v1.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
working-directory: "frontend"
```


See the [actions tab](https://github.com/mattallty/jest-github-action/actions) for runs of this action! :rocket:
See the [actions tab](https://github.com/EkoCommunications/jest-github-action/actions) for runs of this action! :stars:

10 changes: 9 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -16,6 +16,14 @@ inputs:
description: "Comment PRs with code coverage"
required: false
default: "true"
coverage-artifact-save:
description: "Save code coverage to files"
required: false
default: "false"
coverage-results-file:
description: "Code coverage to file path"
required: false
default: "coverage-results.json"
changes-only:
description: "Only run tests on changed files (over base branch)"
required: false
@@ -25,5 +33,5 @@ inputs:
required: true

runs:
using: "node12"
using: "node16"
main: "dist/index.js"
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ import { exec } from "@actions/exec"
import filter from "lodash/filter"
import flatMap from "lodash/flatMap"
import map from "lodash/map"
import { readFileSync } from "fs"
import { readFileSync, writeFileSync } from "fs"
import strip from "strip-ansi"
import table from "markdown-table"

@@ -41,6 +41,16 @@ export async function run() {
// Parse results
const results = parseResults(RESULTS_FILE)

// Write results to file
if(shouldSummitCoveragetoArtifact()){
const resultsFile = core.getInput("coverage-results-file", { required: false })
if (resultsFile) {
const resultsFilePath = join(CWD, resultsFile)
console.debug("Writing results to file: %s", resultsFilePath)
writeFileSync(resultsFilePath, JSON.stringify(results))
}
}

// Checks
const checkPayload = getCheckPayload(results, CWD)
await octokit.checks.create(checkPayload)
@@ -92,6 +102,10 @@ function shouldRunOnlyChangedFiles(): boolean {
return Boolean(JSON.parse(core.getInput("changes-only", { required: false })))
}

function shouldSummitCoveragetoArtifact(): boolean {
return Boolean(JSON.parse(core.getInput("coverage-artifact-save", { required: false })))
}

export function getCoverageTable(
results: FormattedTestResults,
cwd: string,
@@ -156,7 +170,7 @@ function getCheckPayload(results: FormattedTestResults, cwd: string) {
function getJestCommand(resultsFile: string) {
let cmd = core.getInput("test-command", { required: false })
const jestOptions = `--testLocationInResults --json ${
shouldCommentCoverage() ? "--coverage" : ""
shouldCommentCoverage() || shouldSummitCoveragetoArtifact() ? "--coverage" : ""
} ${
shouldRunOnlyChangedFiles() && context.payload.pull_request?.base.ref
? "--changedSince=" + context.payload.pull_request?.base.ref

0 comments on commit 723894d

Please sign in to comment.