Codecov

Contents

Discover how to see test progress over time using Codecov.

Intro

Codecov is a nice reporting tool that can be used to visualize your test coverage over time and across multiple projects. It integrates nicely with GitHub and other CI/CD tools.

Command line

To get a test coverage report when running tests, simply add the --coverage option:

npx jest --coverage

package.json

To get a coverage report every time you run npm test. Add the --coverage option in the package.json scripts section like this:

package.json
{
  "scripts": {
    "test": "jest --no-cache --coverage"
  }
}

This also enables the npm test in the Github action below to produce a coverage report for Codecov.

GitHub action

Here is an example of how to set up a GitHub action to automatically send your test coverage to Codecov:

Make sure the npm test command produces a coverage report. This is done by adding the --coverage option to the jest command as described above
.github/workflows/coverage.yml
name: Coverage

on: [ push ]

jobs:

  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Node
        uses: actions/setup-node@v4
        with:
          node-version: '20.x'
          registry-url: 'https://registry.npmjs.org'

      - name: Install dependencies
        run: npm ci --ignore-scripts

      - name: Test
        run: npm test

      - uses: codecov/codecov-action@v4

Summary

With the above setup, you will get a nice test coverage report every time you run npm test and also every time you push to GitHub. The report will be sent to Codecov where you can visualize it and track your progress over time.

The final chapter will demonstrate how to colocate tests with the source code.


Contents

Contents

AI-powered search

Juke AI