How to Run Tests Only for Changed Files with GitHub Actions
Jest --changedSince runs only the tests related to files changed since a base ref, not the whole suite.
Fetch the base branch, then run jest --changedSince=origin/main. Jest uses its dependency graph to run only tests related to the changed files, which speeds up PR runs while full suites still run on main.
Steps
- Check out with
fetch-depth: 0so the base branch is available for diffing. - Fetch the base branch explicitly (for example
origin/main). - Run
jest --changedSince=origin/mainto limit to affected tests.
Workflow
.github/workflows/ci.yml
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: git fetch origin main
- run: npx jest --changedSince=origin/mainGotchas
- Without
fetch-depth: 0, the base ref is missing and the diff fails. - Always run the full suite on the default branch so nothing slips through the change filter.
Related guides
How to Split a Test Suite into Parallel Jobs with GitHub ActionsSpeed up CI by splitting a test suite into independent GitHub Actions jobs (unit, integration, e2e) that run…
How to Automatically Retry Flaky Tests with GitHub ActionsRe-run only the failed tests in a Jest suite on GitHub Actions with jest.retryTimes or jest --onlyFailures, s…