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.
The workflow consists of two main jobs:
-
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. -
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.
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