💸Save up to $132K/month in CI costs!👉 Try Free
Skip to main content
← Back to workflows

How to Track Code Coverage in Every Pull Request in GitHub Actions

ArtiomTr/jest-coverage-report-action -
GitHub Action
v2.3.0
487
Contributors
Contributor - ArtiomTr
Categories

Optimize Your CI/CD Pipeline

Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.

45% Faster Builds
60% Cost Reduction
Usage
name: 'coverage'
on:
pull_request:
branches:
- master
- main
jobs:
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ArtiomTr/jest-coverage-report-action@v2

jest-coverage-report-action logo

jest-coverage-report-action

Track your code coverage in every pull request.


The Jest Coverage Report action uses Jest to extract code coverage and comment on it on pull requests. Here are its features: it reports code coverage information within every pull request, denies pull requests with a lower threshold than the target coverage, and compares coverages to the base branch. Additionally, it includes spoiler comments for all newly covered files and displays spoilers in comments for files where coverage was lowered.

The action also provides failed tests and hidden line annotations.

How to Handle Forks in Case of No Write Permission

If you encounter the "Resource not accessible by integration" error, change the trigger action to pull_request_target:

name: 'coverage'
on:
pull_request_target:
branches:
- master
- main

jobs:
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ArtiomTr/jest-coverage-report-action@v2

This results in worse DX: you can only test the action when merged into your main branch.

How to Use a User-defined Token

By default, this action uses github.token to publish reports to your PR. You can overwrite this by specifying:

with:
github-token: ${{ secrets.SECRET_TOKEN }}

How to Set Threshold

This method is useful to support Jest's coverageThreshold property. Add this to your jest.config.js file:

module.exports = {
coverageThreshold: {
global: {
lines: 80,
},
},
};

How to Use a Custom Working Directory

If you want this action to work in a custom directory, define working-directory:

with:
working-directory: <dir>

How to Personalize the Test Script

This action appends necessary flags to your test script. The default script is:

npx jest

You can use a different package manager:

with:
test-script: yarn jest

How to Use with Yarn, Pnpm, or Bun

Declare the package manager:

with:
package-manager: yarn

How to Use Existing Test Report(s)

Pass the file path of the current report.json:

with:
coverage-file: ./coverage/report.json
base-coverage-file: ./coverage/master/report.json

How to Compare the Coverage Features

Manually collect coverage to report.json and specify these options:

with:
coverage-file: report.json
base-coverage-file: report.json

How to Skip Steps

You can skip steps using the skip-step option:

with:
skip-step: all

How to Modify Annotations

Set the annotations option:

with:
annotations: none

How to Make the Report Title Readable

If you are running this action multiple times, use the custom-title field to distinguish reports:

with:
custom-title: Backend Test Coverage Report

How to Run the Report Output

A comment is appended by default to a pull request or commit. To specify other actions for publishing the report, use output: report-markdown:

- uses: ArtiomTr/jest-coverage-report-action@v2
id: coverage
with:
output: report-markdown
- uses: marocchino/sticky-pull-request-comment@v2
with:
message: ${{ steps.coverage.outputs.report }}

How to Deal with the Pull Request Numbers

Pass the prnumber to the action for push events:

name: 'coverage'
on:
push:
branches:
- master
- main

jobs:
coverage:
permissions:
checks: write
pull-requests: write
contents: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: jwalton/gh-find-current-pr@v1
id: findPr
- uses: ArtiomTr/jest-coverage-report-action@v2
with:
prnumber: ${{ steps.findPr.outputs.number }}