Skip to content

Github actions running automated testing with jest shard feature and coverage merge for accelerated test execution.

Notifications You must be signed in to change notification settings

hugosanga/jest-shard-workflow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub Action Workflow for Automated Tests with Jest Sharding and Coverage Merge

This repository demonstrates an example GitHub Action workflow for running automated tests using Node.js Jest, splitting the tests using Jest's shard feature, and then merging the coverage of each test run.

📋 Workflow Overview

The workflow consists of two main jobs:

  1. run-tests: This job runs the tests in parallel shards to speed up the testing process. It utilizes the matrix strategy to run tests on different shards simultaneously. If any shard fails, the workflow stops.

  2. report-coverage: This job runs after the tests are completed. It downloads the coverage artifacts generated by the run-tests job, merges them, and generates a final coverage report.

🏗️ Workflow Structure

name: Automated Tests
on:
  pull_request:

jobs:
  run-tests:
    runs-on: ubuntu-latest
    continue-on-error: false
    strategy:
      fail-fast: true
      matrix:
        shard: [1, 2]
    steps:
      # Checkout code
      - uses: actions/checkout@v3
      # Set up Node.js environment
      - uses: actions/setup-node@v3
        with:
          node-version: '16.13.1'
      # Install dependencies
      - name: Install dependencies
        run: yarn install --frozen-lockfile
      # Run tests with Jest shard feature
      - name: Run tests (${{ matrix.shard }}/${{ strategy.job-total }})
        run: yarn test --shard=${{ matrix.shard }}/${{ strategy.job-total }} --coverage
      # Rename coverage file for each shard
      - name: Rename coverage file
        run: mv coverage/coverage-final.json coverage/${{matrix.shard}}.json
      # Upload coverage artifacts
      - uses: actions/upload-artifact@v3
        with:
          name: coverage-artifacts
          path: coverage/

  report-coverage:
    runs-on: ubuntu-latest
    needs: [run-tests]
    steps:
      # Checkout code
      - uses: actions/checkout@v3
      # Download coverage artifacts
      - uses: actions/download-artifact@v3
        with:
          name: coverage-artifacts
          path: coverage
      # Merge code coverage from shards
      - name: Merge code coverage
        run: npx nyc merge coverage/ coverage/coverage-final.json
      # Generate final coverage report
      - name: Generate coverage final report
        run: npx nyc report --reporter lcov --reporter text -t coverage --report-dir coverage

🛠️ Example Pull Requests

  • PR #1: Demonstrates the workflow stopping on test failure. A test is intentionally modified to fail.
  • ✔️ PR #2: Demonstrates the coverage merge working. A new test is added, tests are split into shards, and coverage increases.

About

Github actions running automated testing with jest shard feature and coverage merge for accelerated test execution.

Topics

Resources

Stars

Watchers

Forks