Get code coverage reports with Github Actions and Codecov
Tags: coverage, jest, github, testing
Published: 2020-10-08 09:23:35

---
title: Get code coverage reports with Github Actions and Codecov
published: true
description: Github Actions + Codecov = happy badges
tags: coverage, jest, github, testing
//cover_image: https://direct_url_to_image.jpg
---

TLDR: **_[Show me the money](#the-money)_**

---

So, you've spent some time getting tests set up on your project.  How do you show the world?

A popular service is [Codecov](https://codecov.io), and you'll probably recognise their badges from Github repositories:

![100% coverage on Codecov](https://dev-to-uploads.s3.amazonaws.com/i/e8jbt864adnofe6uk8np.png)

### However

Codecov is a _static_ analysis tool, which means you have to upload reports that have already been tested.  One option is to commit your `coverage` folder, but this is a bad idea:

- You have to run your tests before _every_ commit.  Chances are you'll forget, and your coverage will be out of date.
- You won't be able to edit away from your main computer (i.e. you can't update the tests from your phone).
- It isn't automated enough, and everyone knows more automation is better.
- Probably loads of bad things.

# Github Actions to the rescue!

[Github Actions](https://github.com/features/actions) is a fairly new (I think?) feature that allows you to add [CI](https://en.wikipedia.org/wiki/Continuous_integration) to your project.

There's a few [default templates](https://github.com/actions/starter-workflows), but unfortunately none that I could find for running tests and sending the reports over to Codecov.

## Set up a Github Action

Github actions are stored in `.github/workflows`, and are `YAML` files that specify how the job should run.  [Check out the docs to find out more](https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions).

We'll set up a simple task by adding a file called `codecov-test-suites.yml` and adding the following:

``` yml
# .github/workflows/codecov-test-suites.yml

name: Run Test Suites

on: [push]
```

This sets up a new workflow that triggers for every push.

Now we need to make it do things!  You need to add a job to this file, which does the following:

- Fetches the files
- Installs the dependencies
- Runs the test
- Uploads the test to Codecov

This is easy.  Add this to the `codecov-test-suites.yml` file:

``` yml
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2 # Check out your repository
    - run: yarn # Install dependencies
    - run: yarn test --coverage # Run test
    - run: bash <(curl -s https://codecov.io/bash) # Upload to Codecov
```

Your test should run, but apparently you have 0% coverage.  What?

![Codecov can't process your report](https://dev-to-uploads.s3.amazonaws.com/i/ihp2itmubuil4q942rl5.png)

It turns out the tests are run in a weird folder, and this doesn't tie up with your repository structure, so Codecov gets confused.

To fix this, you need to add a `codecov.yml` file to your project root.  In here, we'll specify how to fix the file paths:

``` yml
# codecov.yml

fixes:
  - "/home/runner/work/<project name>/<project name>/::" # Correct paths
```

Edit `<project name>` to your repository name from Github, and commit this.

**Huzzah!** Github Actions should now be automagically running tests every time you push and sending to Codecov!

Be sure to head to Codecov's badge settings and get that sweet SVG goodness:

![Badges badges badges badges badges!](https://dev-to-uploads.s3.amazonaws.com/i/95llf3p3b462lavs73yc.png)

Remember that 100% badge from earlier?

![Can't get enough of that 100%](https://dev-to-uploads.s3.amazonaws.com/i/e8jbt864adnofe6uk8np.png)

---

# The money

``` js
// package.json

{
  // Make sure jest creates lcov reports
  "jest": {
    "collectCoverage": true,
    "coverageReporters": ["html", "lcov"]
  }
}
```

``` yml
# .github/workflows/codecov-test-suites.yml

name: Run Test Suites

on: [push]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2 # Check out your repository
    - run: yarn # Install dependencies
    - run: yarn test --coverage # Run test
    - run: bash <(curl -s https://codecov.io/bash) # Upload to Codecov
```

``` yml
# codecov.yml

fixes:
  - "/home/runner/work/<project name>/<project name>/::" # Correct paths
```