Originally published on anton.qa.

According to recent reports, two readers on DEV added a good point to the -exclusive- lock article. Playwright 1.63 added test locks. Version 1.65 added the built-in -exclusive- lock. It makes one test run alone, such as a server restart. But a lock only sees its own Playwright run. What happens when a suite is split into shards?

It has been revealed that their advice was to give each shard its own server and port. One reader also suggested keeping each shard's test data under its own name prefix.

The results below come from real runs on Playwright 1.65.1. You'll see the failure, two fixes, and one small trap in the config.

Industry observers note that sharding splits one test suite into parts, called shards. You pick a part with a flag like --shard=1/2. CI is the system that runs your tests on each push. There, each shard is usually its own job. (Related: Porting fairseq wmt19 translation system to transformer...)

Sources indicate that the test-locks docs say locks work across files, worker processes and projects. Shards aren't on that list. Each shard is a separate run, and it only knows its own tests. (Related: Stop treating agents as one-off projects and start runn...)

In a recent development, the demo reuses the notes server from the last article. Its data lives in memory, so a restart wipes it. For 1.5 seconds after a restart, it answers 503. That status means "busy, try again later.".

According to recent reports, one thing changed. The server now takes its port as an argument. A port is the number a server listens on.

Save it as server.mjs. The tests are the same seven as last time. Six tests save a note, wait one second, then read it back. The seventh restarts the server and waits until it answers again.

Recent findings suggest that the restart test now has a tag, @restart. A tag is a label you can filter on.

Save it as tests/notes.spec.mjs. This time the restart comes first in the file. That makes the overlap happen on every run, as you'll see below.

Recent findings suggest that the first config sends every shard to one server that is already running. Think of a shared test server in CI, often called staging.

Save it as playwright.shared.config.mjs. fullyParallel: true lets Playwright split single tests between shards, not just whole files. Start the server in one terminal:.

Then run both shards at once from a second terminal:.

Industry observers note that the & starts each command in the background, so both shards run together. These commands are for macOS and Linux.

It has been revealed that three runs ended the same way. Shard 1 ran 4 tests, and all passed. Shard 2 ran notes d, e and f, and all 3 failed. Every failure received 503, sometimes on the save and sometimes on the read-back.

In a recent development, here is the first run. Times are seconds from the first test's start, across both shards.

Inside shard 1, the lock worked. The restart ran alone, and notes a, b and c waited for it. Shard 2 never saw that lock. Its three tests ran during the restart and got 503.

In a recent development, in another version of the test file, the restart came fourth instead of first. Shard 1 then ran notes a, b and c before the restart. Shard 2 passed in all three runs.

But its last test ended only 0.03 to 0.07 seconds before the restart began. Nothing kept the two shards apart. Shard 2 just happened to finish first.

GitHub-hosted runners give every job a fresh, separate runner. GitHub's workflow docs say "each job runs in a fresh instance of a runner image". Say your config starts the app with webServer. That option starts a server before the tests run. Then each CI shard already gets its own copy.

Shards share a server in two common cases:.

Industry observers note that the first case has a simple fix. The second needs a different blueprint.

Give each shard its own port, and let each shard start its own server. Read the shard number from one place. Here it comes from an environment variable, a named value set before a command. I called it SHARD.

Save it as playwright.config.mjs. SHARD=1/2 now sets two things. The config's shard option picks the tests. Port 3124 goes to the server and to baseURL, the address the tests call. Without SHARD, the whole suite runs on port 3123, so stop the first server before you try that.

It has been revealed that all three runs passed: 4 tests in shard 1 and 3 in shard 2. In the demo's JSON reports, the two server commands were node server.mjs 3124 and node server.mjs 3125.

Recent findings suggest that the restart still overlapped notes d, e and f in time. But it restarted a different server, so nothing failed.

In a recent development, why not pass --shard on the command line, like the sharding guide does? You can, but then the port won't follow it.

Industry observers note that one run set SHARD=1/2 and also passed --shard=2/2. The command-line flag won, so Playwright ran shard 2's tests. The server still used port 3124, the port for shard 1. With one value in one place, the two can't disagree.

In a recent development, on one machine, two servers need two ports. In one more test, both shards started a server on port 3123. In all three runs, one shard stopped before running any test:.

EADDRINUSE means the port is already taken. The other shard passed. Which shard lost changed from run to run.

Here is how I'd pass SHARD from a GitHub Actions matrix. A matrix runs one job per value, here one job per shard. The shape follows the Playwright sharding guide.

It has been revealed that this file passed actionlint, a checker for workflow files. It was not run in CI.

In a recent development, if your tests open a browser, add npx playwright install --with-deps after npm ci. The demo skips it, because it only uses the request fixture, a built-in HTTP client.

Sometimes you can't copy the server. It might be a shared staging server, or a database every job uses.

Then keep the restart out of the shards. Run it once, alone, after they finish. The @restart tag makes that easy:.

Sources indicate that --grep-invert @restart skips the tagged test. --grep @restart runs only that test. All three runs passed: 3 notes in each shard, then the restart alone. The restart never overlapped another test.

In GitHub Actions, the restart becomes its own job. needs: shards makes it wait for both shards. This file also passed actionlint and was not run in CI.

Two limits apply. If a shard fails, GitHub skips the restart job by default. The needs docs explain this. Also, this only orders jobs inside one workflow run. Another workflow or team can still call the same staging server.

The first reader also suggested a data namespace per shard. A namespace is a prefix that keeps one shard's data apart. That matters when shards share a database.

Watch for one trap. Playwright runs tests in several workers at once. A worker is one test process. A common way to name test data uses its number, parallelIndex. In the demo, both shards had workers 0, 1 and 2. So a name like user-0 would repeat in both shards.

Add the shard number. Inside a test, testInfo.config.shard holds it:.

Recent findings suggest that in a small test file, six tests printed this name in two shards. In three runs, the names went from s1-w0 to s2-w2, with no repeats. That held with --shard on the command line and with SHARD in the config.

The name belongs to a worker, not a test. Tests that run one after another in a worker share it. Two workers running at once get different names, even in different shards.

Two workflows might use the same database at once. Then I'd add the CI run ID too. GitHub sets it as GITHUB_RUN_ID. A re-run keeps that ID, so I'd add GITHUB_RUN_ATTEMPT as well. The reader also suggested cleaning up only your own namespace at the end. Then a failed job can't delete another shard's data. I agree, but this demo has no database, so neither idea was run here.

A namespace doesn't help with a restart, though. Notes d, e and f already had their own names. They still failed, because the restart took down the whole server.

The demo ran on macOS with Node.js 24 and Playwright 1.65.1. Make a folder with the four files from this article:.

Run the commands from each section. You should see these results:.

Sources indicate that exact seconds will differ on your machine. The demo's waits are set by hand, so the overlap happens every time. It shows what a shared restart does to another shard. It doesn't measure how often that happens in a real suite.

Sources indicate that thanks to the two readers on DEV who spotted this. Their per-shard server advice held up in every run here.

I'm Anton. I build AI tools and measure how fast they really run. Former Apple engineer. I make ollamatps.com and write about building and measuring AI at anton.qa.

→ Read original article