Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check test coverage percentage in Github Actions

I have a react project published to GitHub, and trying to set up GitHub Actions.

I want to add one step to check if the unit test coverage passes 65% (have to pass it to go through CI/CD process successfully).

This is what I've tried:

build:
    name: Test
    steps:
      - name: Coverage
        run: npm run jest-coverage

I need help on following items:

  • How can I check if the coverage percentage passes 65%?
  • How to make the process fail if the coverage is lower than 65%?
  • How can I make it rerun it whenever there's a new commit pushed?
like image 719
doobean Avatar asked Sep 06 '25 03:09

doobean


1 Answers

Assuming that your npm script jest-coverage runs jest with coverage enabled

Requiring 65% test coverage can be achieved with the jest configuration coverageThreshold.

Add this to your jest.config.js:

{
  ...
  "jest": {
    "coverageThreshold": {
      "global": {
        "branches": 65,
        "functions": 65,
        "lines": 65,
        "statements": 65
      }
    }
  }
}

As for re-running your job when new code is pushed, you want to trigger your workflow on push, like so:

on: push

jobs:
  ...

Depending on how you're working with branches and pull requests, the push could also be replaced with pull_request.

like image 81
rethab Avatar answered Sep 07 '25 23:09

rethab