Run Tests with Jest

Automating Jest Tests and Coverage Reporting Using GitHub Actions and A How-To Guide #

This guide will walk you through the process of running your tests with Jest and generating a coverage report using GitHub Actions. By the end of this guide, you will have a functioning pipeline that executes your Jest tests and produces detailed coverage reports for each pull request.

Prerequisites #

Before we get started, make sure you have the following:

  1. A GitHub repository with a JavaScript or TypeScript project.
  2. Jest installed as a development dependency in your project. If not already installed, you can add it using npm:
npm install --save-dev jest

or using yarn:

yarn add --dev jest
  1. A jest configuration file (jest.config.js) in the root directory of your project. Here’s an example of a simple Jest configuration for a TypeScript project:
module.exports = {
    preset: 'ts-jest',
    testEnvironment: 'node',

Setting Up the GitHub Actions Workflow #

Now that we have the prerequisites sorted out, let’s proceed with setting up our GitHub Actions Workflow.

Step 1: Create the Workflow File #

Create a new file in your repository at .github/workflows/coverage.yml and open it for editing.

Step 2: Define the Workflow #

Copy the following code into the workflow file:

name: GitHub Actions

on: [pull_request]

    runs-on: ubuntu-latest

    - uses: actions/checkout@v3
    - name: Use Node.js 16.x
      uses: actions/setup-node@v3
        node-version: '16.x'
    - name: Install dependencies
      run: npm ci
    - name: Run Jest tests and generate coverage report
      run: npm test -- --coverage --coverageReporters="json-summary" --coverageReporters="json"
    - name: Archive coverage report (json-summary)
      uses: actions/upload-artifact@v3
        name: coverage-summary-report
        path: coverage/coverage-summary.json
    - name: Archive coverage report (json)
      uses: actions/upload-artifact@v3
        name: coverage-report
        path: coverage/coverage-final.json

This workflow will:

  1. Checkout your repository.
  2. Set up Node.js environment.
  3. Install your project dependencies.
  4. Run your Jest tests and generate a coverage report.
  5. Archive the JSON summary and JSON final coverage report as artifacts in the workflow run.

Note: The Job name and artifact names are very important

In order for to successfully pull your code coverage reports, the following requirements must be met:

  1. All artifacts you want to analayze are included in a Job called “Check Coverage”
  2. Coverage Summary Report artifact is named “coverage-summary-report”
  3. In depth coverage report is named “coverage-report”

Save the file and push your changes to GitHub.

Testing Your Setup #

You’re all set! From now on, whenever a pull request is opened or updated, this workflow will run, executing your Jest tests and producing a detailed coverage report.

Remember to check the “Artifacts” section in your GitHub Actions run to view the generated coverage reports.

Now, you are ready to use these reports with tools like to analyze your test coverage and generate tests for uncovered code. Happy testing!