Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readTesting

Run changed Playwright tests first in CI

Run Playwright’s changed-test selection against the pull request base for quick feedback, then run the full suite to cover browser-only dependencies.

When a pull request changes a test helper, run the tests that import it before the full browser suite. Playwright 1.46 added --only-changed for this first pass.

Compare against a base Git ref

Give Playwright the target branch ref or the base commit supplied by your CI event. In this example the local checkout already has origin/main and the relevant history. If the pull request targets a release branch, pass that branch's base instead.

set -e
base_ref=origin/main
git rev-parse --verify "$base_ref^{commit}"
pnpm exec playwright test --only-changed="$base_ref"
pnpm exec playwright test

Playwright runs the selected files first and the full suite after that pass succeeds.

Use the explicit ref for a CI checkout where the changes are committed. The bare flag is for changes relative to the checkout's latest commit. If Git cannot resolve the ref, fetch the base commit as part of checkout. A shallow clone with just the head commit may lack that base. Use the event’s base SHA if the comparison needs to stay fixed while the target branch moves.

Follow the import edge

Suppose checkout.spec.ts imports cart-helper.ts. Changing the helper can select the checkout spec even if its source has no diff. A second spec may visit the checkout through page.goto and rely on the same behavior without importing the helper. Playwright cannot infer that relationship from the URL.

diagram

What the preflight can see

The changed helper reaches checkout.spec.ts through an import, while an app route can connect it to a test the preflight misses.

Playwright documents changed-test selection as a heuristic. Configuration, server behavior and files the browser requests can affect tests that have no import edge to the diff. Treat the selected files as an early check and keep the unfiltered run as the required result.

Check the cost of a second run

On a green pull request, the selected tests run again in the full suite. Use the extra pass where it gives useful failure feedback before the longer command. If it often selects most of the suite, compare how long it takes to see the first failure with and without the preflight before keeping it.