Playwright can run only the tests a pull request changed, with one flag. On 8 seeded pull requests against a 12-test suite, --only-changed caught 13 of the 27 tests the bugs broke. In 5 of the 8 pull requests it ran 0 tests, printed nothing and exited 0. 4 of those 5 had a bug that failed between 1 and 9 tests on the full suite.

This guide sets up both ways to run only the affected tests on a pull request: Playwright's --only-changed, and a map with playwright-affected, a tool I maintain. The map caught 27 of 27 and ran 67 of the 96 tests the full suite ran. The shop, the tests, the map, the workflow and the raw output of every run are in the PoC repo.

The suite

  • A small static shop in app/: search, cart and checkout in three JavaScript files, a stylesheet, and a static file server that Playwright starts with webServer.
  • 12 tests in 4 spec files, one tag per file: @search (3), @cart (3), @checkout (4) and @smoke (2).
  • The search, cart and checkout specs use page objects in tests/pages/. Every page object extends base-page.ts. The smoke spec uses no page object.
  • Playwright 1.65.1 with Chromium, playwright-affected 0.1.2, Node v24.14.1, macOS on arm64. workers: 4, no retries.

Each scenario is one commit on top of main, and main is the base for both methods. 7 of the 8 commits add a bug:

Scenario Changed file The change
test-edit tests/search.spec.ts A spec edit with a wrong expectation
page-object tests/pages/checkout-page.ts A page object with a wrong locator
base-page tests/pages/base-page.ts The base page opens the wrong URL
app-checkout app/checkout.js The coupon comes off as pounds, not percent
app-search app/search.js Search shows only the first 3 products
app-css app/styles.css A rule hides the Place order button
config playwright.config.ts JavaScript is turned off for every test
docs README.md A README edit only

Every scenario ran three ways: the full suite, --only-changed, and playwright-affected. "Broken" means a test failed on the full suite. "Caught" means the method ran that test and saw it fail.

Option 1: --only-changed

--only-changed takes a git ref and runs the test files that changed between that ref and HEAD, plus the test files that import a changed file:

npx playwright test --only-changed=origin/main 

On a pull request in GitHub Actions, check out the full history so the base branch is there:

- uses: actions/checkout@v7 with: fetch-depth: 0 - run: npx playwright test --only-changed=origin/$GITHUB_BASE_REF 

The results:

Scenario Full suite --only-changed
test-edit 12 run, 1 failed 3 run, 1 caught, exit 1
page-object 12 run, 2 failed 4 run, 2 caught, exit 1
base-page 12 run, 10 failed 10 run, 10 caught, exit 1
app-checkout 12 run, 1 failed 0 run, 0 caught, exit 0
app-search 12 run, 2 failed 0 run, 0 caught, exit 0
app-css 12 run, 2 failed 0 run, 0 caught, exit 0
config 12 run, 9 failed 0 run, 0 caught, exit 0
docs 12 run, 0 failed 0 run, 0 caught, exit 0

It works for changes inside the test code. A changed spec selected its own file. A changed page object selected the specs that import it: 4 checkout tests for checkout-page.ts, and 10 tests in 3 spec files for base-page.ts, which every page object extends. It caught every broken test in those 3 scenarios.

It does not see the app. The tests reach the shop over HTTP and import nothing from app/, so the 3 app changes selected 0 tests. That is the normal shape of an end-to-end suite. A change to playwright.config.ts also selected 0 tests, though it broke 9 of 12.

The empty runs did not fail. With 0 tests selected, --only-changed printed nothing and exited 0. Two controls on main show the difference. A --grep that matches no test printed Error: No tests found and exited 1. --only-changed with no change printed nothing and exited 0. So a pull request that changes only app code gets a green check from a run that tested nothing. The empty shards post found the same exit 0 on Playwright 1.64.1 and 1.65.1.

The Playwright CI docs call --only-changed a heuristic that might miss tests. Their example runs it as a first step for fast feedback, then runs the full suite. Used that way, the full suite still runs the tests it skips. Used alone, it missed 14 of 27 broken tests here.

Option 2: a map with playwright-affected

playwright-affected maps changed files to tags, test paths or Playwright projects, and emits one --grep. When it cannot decide, it runs the full suite. This is the map for the shop, e2e-map.yml:

version: 1 test_dir: tests always_run_tags: ["@smoke"] full_run_triggers: - package-lock.json - playwright.config.ts - e2e-map.yml - tests/pages/base-page.ts - app/server.mjs - app/index.html - .github/workflows/** ignore_paths: - "**/*.md" - scenarios/** - scripts/** - results/** - video/** mappings: # cart and checkout tests add items through the search page  - paths: ["app/search.js", "tests/pages/search-page.ts"] tags: ["@search", "@cart", "@checkout"] - paths: ["app/cart.js", "tests/pages/cart-page.ts"] tags: ["@cart", "@checkout"] - paths: ["app/checkout.js", "tests/pages/checkout-page.ts"] tags: ["@checkout"] unmapped_behavior: full 

Each changed file goes through the steps in order, and the first match decides:

  1. full_run_triggers runs the full suite: the config, the lockfile, the map itself, the base page, the server, index.html and the workflows.
  2. A changed spec file selects itself.
  3. ignore_paths skips the file.
  4. mappings add tags. The app files and their page objects map to the tests that use them.
  5. Anything else runs the full suite, because unmapped_behavior is full.

always_run_tags adds the 2 @smoke tests to every selection.

To run it, list the tests, select, then pass the grep to Playwright:

PLAYWRIGHT_JSON_OUTPUT_NAME=list.json npx playwright test --list --reporter=json > /dev/null npx playwright-affected select --config e2e-map.yml --base origin/main --list list.json --out result.json GREP=$(jq -r '.grep // empty' result.json) npx playwright test ${GREP:+--grep "$GREP"} 

When result.json has mode: full, grep is null, and the last line runs the full suite.

The results:

Scenario Full suite playwright-affected
test-edit 12 run, 1 failed 5 run, 1 caught (selected)
page-object 12 run, 2 failed 6 run, 2 caught (selected)
base-page 12 run, 10 failed 12 run, 10 caught (full: global_trigger)
app-checkout 12 run, 1 failed 6 run, 1 caught (selected)
app-search 12 run, 2 failed 12 run, 2 caught (selected)
app-css 12 run, 2 failed 12 run, 2 caught (full: unmapped_paths)
config 12 run, 9 failed 12 run, 9 caught (full: global_trigger)
docs 12 run, 0 failed 2 run, 0 caught (selected)
  • It caught 27 of 27, and every pull request with a bug exited 1.
  • It ran 67 tests across the 8 pull requests; the full suite ran 96.
  • It ran all 12 tests in 4 pull requests: twice for a full-run trigger, once for an unmapped file (app/styles.css), and once because the mapping for app/search.js selects all 4 tags.
  • The docs-only pull request ran the 2 smoke tests.

The app-search row shows why the map has the search page selecting cart and checkout tests too. Showing only 3 products broke applies a coupon, a checkout test, because it adds the 4th product. A map that sent app/search.js to @search alone would have missed it.

The app-css row shows the safety net. Nobody mapped the stylesheet, so the change ran the full suite and caught both broken checkout tests.

The workflow

This is the workflow in the repo. On a pull request, it selects and runs the affected tests. On main, there is no result.json, so it runs the full suite:

name: Playwright Tests on: push: branches: [main] pull_request: branches: [main] workflow_dispatch: jobs: playwright-tests: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 with: # select diffs against the base branch, so it needs the history  fetch-depth: 0 - uses: actions/setup-node@v7 with: node-version: lts/* - run: npm ci - run: npx playwright install --with-deps chromium - name: Select the affected tests if: github.event_name == 'pull_request' env: BASE_REF: ${{ github.base_ref }} run: | rm -f result.json PLAYWRIGHT_JSON_OUTPUT_NAME=list.json npx playwright test --list --reporter=json > /dev/null npx playwright-affected select --config e2e-map.yml --base "origin/$BASE_REF" --list list.json --out result.json jq '{mode, full_reason, selected, total, grep}' result.json - name: Run the affected tests, or all of them on main run: | GREP="" if [ -f result.json ]; then GREP=$(jq -r '.grep // empty' result.json); fi npx playwright test ${GREP:+--grep "$GREP"} 
  • fetch-depth: 0 gives select the base branch history.
  • rm -f result.json stops a later step from reading a stale selection.
  • The jq line prints the mode, the reason for a full run and the counts in the job log, so a reviewer can see what ran and why.

Pull request #1 in the repo is the app-checkout bug. In GitHub Actions (job log), select chose 6 of 12 tests, and applies a coupon failed: the total was £23.90, where the test expects £21.60.

What didn't work

  • One 12-test suite with one tag per spec file, and a map written for this app with all of it in view. A real map starts with fewer files mapped, so more pull requests run the full suite.
  • The map is only as good as the person who writes it. The app-search row caught both broken tests only because I knew the checkout tests use the search page.
  • Each scenario changes one file. Larger pull requests touch more mappings, and any unmapped file runs the full suite.
  • I did not compare wall times. The matrix ran once, and each run is mostly browser and server start-up.

What I'd do

  • Never use --only-changed as the only test run on a pull request. Run it first for fast feedback, then the full suite, as the Playwright docs show.
  • For a selection you can trust on its own, map the app to tags and keep unmapped_behavior: full.
  • Put the config, the lockfile, shared page objects and the map itself in full_run_triggers.
  • Keep the full suite on main or the merge queue, and on a schedule.
  • Before you make selection blocking, run it in shadow mode next to the full suite. The playwright-affected README has the steps.

For selection on a unit-test suite, see Code Graph Test Selection.

The shop, the tests, the map, the workflow and the raw results: poc-playwright-affected-tests-pr.

Originally published at https://garyparker.dev/blog/run-only-affected-playwright-tests-pull-request