Skip to content
Latchkey

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: 0 so the base branch is available for diffing.
  • Fetch the base branch explicitly (for example origin/main).
  • Run jest --changedSince=origin/main to 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/main

Gotchas

  • 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

Run this faster and cheaper on Latchkey managed runners. Start free →