Skip to content

Test Data Prepared Before the Run, Not Inside It

Adityo Guni Waluyo

A deep-CRUD E2E suite kept leaving rows behind until data preparation moved ahead of the run and cleanup became a verified sweep ending at zero.

TL;DR

A deep-CRUD suite's final COUNT check kept failing because leftover rows survived cleanup. Scattering deletes inside tests made things worse; the real fix was preparing data upfront with shared fixtures and owning cleanup centrally. A two-phase afterAll sweep ending in a zero-count check, plus sequential workers, makes failures point at the test, not the product.

The first full run of the deep-CRUD suite looked clean until the last step. The afterAll sweep trashed every entity the run had created, called permanent delete on each one, then ran the final COUNT check. That check had to return zero. It returned rows instead. The same leftovers showed up again on the next run, so this was not residue from an older session. The run itself was leaving rows behind.

One shared entity editor serves all five module groups, so the spec is not written five times. It parameterizes MODULES with the admin route, the public base, and the category label, then loops over the five entries. Each iteration fills every field of the form, uploads an image, saves, and later cleans up.

The first fix attempt assumed the leftovers belonged to individual tests. If every test deletes what it creates, the sweep should find nothing. That turned out to be the wrong guess.

More delete calls inside the tests

Deletes were added inside every test: one right after the create assertion, one more in a teardown step. The suite got harder to read and the COUNT check still came back above zero. Cleanup was now spread across tests that shared one entity editor and one dataset, and a single skipped step stranded a row no later step knew about.

This is the documented isolation rule: state leaked through module-level variables or cross-test dependencies works when tests run in order, but breaks the moment they run in parallel or in a different order; set up everything a test needs in the test or in a fixture [3]. Scattered deletes are that rule in reverse. Cleanup became shared state.

The rows were never the problem. Ownership was.

Fixtures prepared before the spec runs

The change that mattered moved data preparation ahead of the run. A support module, support/dummy.ts, builds the dataset before any test runs, and the PNG fixtures are committed to the repo. They are PIL-generated placeholders, which the commit states plainly, so the run needs nothing external. Names are built once from STAMP = Date.now(), so every row the run creates is recognizable afterwards.

The file field follows the same prepared-then-executed pattern. Point locator.setInputFiles() at the input[type=file] element, and pass multiple files as an array [1]. The fixture path is fixed before the spec starts, so the upload step never has to generate or fetch anything.

Two fields are derived rather than typed: lat and lng get auto-filled from the maps URL after trailing-zero stripping. Assert them with expect.poll. For assertions that need retrying beyond the auto-retrying matchers, use expect.poll or expect.toPass [2].

Reading truth back, and sweeping to zero

Read the entity slug and status back from the admin API instead of guessing from the UI. The API shows what was stored.

The sweep is two phases in afterAll: trash through the API, then permanent delete. It ends with a COUNT check that must equal zero. Check one pitfall here: a DELETE without the Authorization header returns a silent 401. No error body, no stack trace. The row simply stays. If the count ever comes back non-zero, inspect the response status of each sweep call before suspecting the product.

Run the suite solo-sequential with --workers=1. The sweep is a shared operation across the module loop, so concurrency buys nothing and risks interleaving create and delete phases between workers.

The COUNT assertion stays the last line on purpose. A non-zero count fails the run and points at the test, not the product, and that is the failure mode worth having.

---

1. Upload files — Playwright documentation 2. Auto-retrying assertions — Playwright documentation 3. Parallelism & test isolation — Playwright documentation

Related articles