Skip to content

taylorlroberts7/increase-coverage-action

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

Increase Coverage Action

GitHub version Commitizen friendly

This action will dynamically increase your code coverage thresholds after code coverage is reported. Since this action relies on your test runner job to run first (needs: [test]), it will only run if your test coverage meets or beats the coverage thresholds set at the time of the tests being ran.


Table of Contents


Usage

Caching

❗️ This action reads the config and summary files from the GitHub action cache so it must be used in combination with the GitHub cache action.

  • The cache key input used with the actions/cache step must match the key inputs (config-key and summary-key) provided to this action
  • The cache path input used with the actions/cache step must match the path inputs (config-path and summary-path) provided to this action
  • See example below.

Committing Coverage Changes

You can use this action in combination with the Add & Commit action to commit the config file with the updated code coverage thresholds.

  • If you only want to run this action when PR branches are merged, you can add this line to the job block:
    if: github.ref == 'refs/heads/main'
  • See example below
  • Note: If your main branch is protected, you will need to pass a GitHub token to actions/checkout:
    - name: Checkout
      uses: actions/checkout@v2
      with:
        token: ${{ secrets.GH_TOKEN }}
    • In this case, you'll want to prevent action loops on your main branch by telling GitHub to ignore changes to your config json files:
      on:
        push:
          branches:
            - main
            - production
          paths-ignore:
            - "**.config.local.json"

Using This Action with Other Test Coverage Tools

By default, this action works with Jest's code coverage configuration (coverageThreshold object). However, you can pull the coverage thresholds from the config file written by this action and apply them to other coverage configuation tools like IstanbulJS/nyc.


Inputs

Variable Description Example Required?
config-key Cache key used for caching config file in test runner job ${{ runner.os }}-cache-jest-config Yes
config-path Path to your test config file jest.config.local.json Yes
summary-key Cache key used for caching coverage summary file in test runner job ${{ runner.os }}-cache-coverage-summary Yes
summary-path Path to generated code coverage summary JSON file coverage/coverage-summary.json Yes

Outputs

This action will write to the config file passed in (pulled from GH action cache) with the updated coverage threshold values from the coverage summary file.

Original config file (e.g. `jest.config.local.json`)
{
  "coverageThreshold": {
    "global": {
      "branches": 77,
      "functions": 79,
      "lines": 79,
      "statements": 79
    },
    "./src/App.tsx": {
      "branches": 80,
      "functions": 80,
      "lines": 80,
      "statements": 80
    }
  }
}
Coverage summary generated by test run (e.g. `coverage/coverage-summary.json`)
{
  "total": {
    "lines": {
      "total": 630,
      "covered": 461,
      "skipped": 0,
      "pct": 80
    },
    "statements": {
      "total": 740,
      "covered": 543,
      "skipped": 0,
      "pct": 81
    },
    "functions": {
      "total": 140,
      "covered": 58,
      "skipped": 0,
      "pct": 81
    },
    "branches": {
      "total": 321,
      "covered": 182,
      "skipped": 0,
      "pct": 81
    }
  },
  "/Users/taylorroberts/Documents/academic-portal-host/src/App.tsx": {
    "lines": {
      "total": 9,
      "covered": 8,
      "skipped": 0,
      "pct": 80
    },
    "functions": {
      "total": 1,
      "covered": 1,
      "skipped": 0,
      "pct": 80
    },
    "statements": {
      "total": 10,
      "covered": 9,
      "skipped": 0,
      "pct": 80
    },
    "branches": {
      "total": 4,
      "covered": 2,
      "skipped": 0,
      "pct": 80
    }
  }
}
Updated config file written to by increase coverage action (e.g. `jest.config.local.json`)
{
  "coverageThreshold": {
    "global": {
      "branches": 81,
      "functions": 81,
      "lines": 80,
      "statements": 81
    },
    "./src/App.tsx": {
      "branches": 80,
      "functions": 80,
      "lines": 80,
      "statements": 80
    }
  }
}

Examples

Caching in Test Job

Caching test config and coverage summary files in test runner job

test:
  runs-on: ubuntu-20.04
  container: node:16
  needs: [install_dependencies]
  steps:
    - name: Checkout
      uses: actions/checkout@v2
    # 👉 Cache coverage summary here
    - name: Cache Coverage Summary
      uses: actions/cache@v3
      env:
        cache-name: cache-coverage-summary
      with:
        key: ${{ runner.os }}-${{ env.cache-name }}
        path: coverage/coverage-summary.json
    # 👉 Cache test config file here
    - name: Cache Jest Config
      uses: actions/cache@v2
      env:
        cache-name: cache-jest-config
      with:
        key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('jest.config.local.json') }}
        path: jest.config.local.json
    # Run test with coverage reporting on
    - name: Testing code
      run: yarn test:coverage

Running Increase Coverage Action

increase-test-coverage:
  runs-on: ubuntu-latest
  container: node:16
  # 👉 Add the name of your test job here to ensure this runs after
  needs: [install_dependencies, test]
  # 👉 Run this when PR branches are merged to main
  if: github.ref == 'refs/heads/main'
  steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Increase Jest Coverage
      # 🚨 Use most recent version!
      uses: taylorlroberts7/increase-coverage-action@v1.0.0
      with:
        config-key: ${{ runner.os }}-cache-jest-config-${{ hashFiles('jest.config.local.json') }}
        config-path: jest.config.local.json
        summary-key: ${{ runner.os }}-cache-coverage-summary
        summary-path: coverage/coverage-summary.json
    # 👉 Commit changes to your config file (if any changes made)
    - name: Commit Jest Config
      uses: EndBug/add-and-commit@v7
      with:
        default_author: github_actions

Using this action with Istanbul + nyc

  • nyc.config.local.json (Use this file with the increase coverage action)

    {
      "coverageThreshold": {
        "global": {
          "branches": 81,
          "functions": 80,
          "lines": 80,
          "statements": 80
        },
        "./src/App.tsx": {
          "branches": 80,
          "functions": 80,
          "lines": 80,
          "statements": 80
        }
      }
    }
  • nyc.config.js (nyc configuration file)

    const local = require("./nyc.config.local");
    
    const defaultConfig = {
      all: true,
      "check-coverage": true,
      exclude: ["**/__mocks__", "**/__tests__"],
      extends: "@istanbuljs/nyc-config-typescript",
      include: ["src/**"],
      "report-dir": "my-coverage",
    };
    
    // Pull global coverage thresholds from file that the increase coverage action writes to
    module.exports = Object.assign(defaultConfig, local.coverageThreshold.global);

About

GitHub action to increase test coverage

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •