Coder Social home page Coder Social logo

increase-coverage-action_lt24's Introduction

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/[email protected]
      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);

increase-coverage-action_lt24's People

Contributors

taylorlroberts7 avatar semantic-release-bot avatar trellixvulnteam avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.