Blog
Ghostrow Labs

Which row broke which test

How Haunt turns a red test suite into a short list of edge cases: a baseline run, an all-cases run, then groups that are split until each failing test points at the rows behind it.

Edge-case data is easy to generate. The hard part is what comes after: you load a few hundred odd rows, three tests go red, and now you have to work out which of the rows did it. ghostrow haunt --tests does that search for you. This post describes how, using a small project we built for the purpose and the Pro plan's Haunt mode.

What Haunt generates

Haunt first builds a small valid dataset from your schema: 30 rows for the largest table, made the same way as seed data. Edge cases are then derived from that base. For each column, Haunt picks values that its type and limits allow:

  • Text: the exact maximum length, accents and CJK characters, emoji with joiners, right-to-left text, quotes and comment markers, leading and doubled spaces, a line break and a tab, and the empty string. Email columns get a +tag, capitals, an apostrophe and a 64-character local part instead.
  • Numbers: zero, the largest and smallest value of the type, minus one, the smallest positive step of a decimal.
  • Dates and times: a leap day, a time that does not exist when European clocks go forward, one that happens twice when US clocks go back, the Unix epoch and the 32-bit timestamp limit.
  • Whole rows: every optional column NULL at once, a parent with no children, and a parent with 50.

Every case has to stay inside your constraints. Each edge row is a copy of a base row with one value changed, then repaired so that products, copies and orderings still hold. Cases that cannot be made valid are dropped, and the full set is run through the same validator seed uses, so a failing test points at your application code, not at a row your database would have refused. Cases get ids like H001, interleaved across tables, and --cases caps the total (300 by default).

The project

Our test project has two tables, customers and orders, and four tests written with Node's built-in runner. They read the generated CSV files from the folder Haunt passes in GHOSTROW_HAUNT_DIR. Each test encodes an assumption we have seen in real code: emails match ^[a-z0-9.-]+@[a-z0-9.-]+\.[a-z]+$, notes have no line breaks, quantities are whole numbers, display names are printable ASCII.

ghostrow --schema schema.sql --seed 1 haunt \
  --tests "node --test --test-reporter=tap app.test.mjs"

The runs

Haunt does not guess from the test output of one run. It runs your command several times, each time against a different set of cases:

  1. Baseline. Zero cases, just the base data. Any test that fails here is already failing and is excluded from everything that follows.
  2. All cases. Every case at once. A test that fails now but passed in the baseline is a new failure. If there are none, Haunt stops and reports no new failures.
  3. Narrowing. For the new failures only, it groups the cases by column. There are at most eight groups; if there are more columns than that, columns are merged. Each group is a run.

With --db, each run inserts the base rows and the chosen cases in one transaction and deletes exactly those rows afterwards, and the database connection is closed while your tests run. With --schema, the rows are written as CSV for the run and the folder is removed at the end. GHOSTROW_HAUNT_CASES points at the case list, so a test can read what each case was meant to be.

Halving

After the first round, each group that still makes a tracked test fail is split. A group that spans several columns is split by column into two halves. A group that is down to one column is split by case, in two halves. A group that no longer breaks the test is dropped, and a single case that does is blamed. Each test follows its own branch, so a test that fails only because of emails is not re-run with the notes cases.

Here is what that looked like on our project, with output trimmed to the interesting lines:

running   node --test --test-reporter=tap app.test.mjs (baseline: 0 cases)
running   node --test --test-reporter=tap app.test.mjs (all cases: 36 cases)
running   node --test --test-reporter=tap app.test.mjs (narrowing 1: 4 cases)
running   node --test --test-reporter=tap app.test.mjs (narrowing 2: 2 cases)
...
running   node --test --test-reporter=tap app.test.mjs (narrowing 31: 1 case)
ghostrow haunt · schema.sql · 36 edge cases across 2 tables · Pro
  3 tests failed only with edge cases:
  ✗ display names are ASCII for the label printer
      broken by H011, H013, H015, H019 e.g. customers.display_name: accents, CJK and Greek characters
  ✗ every email passes our signup regex
      broken by H001, H003, H005 e.g. customers.email: email with a +tag and a subdomain
  ✗ notes fit on one line of the support screen
      broken by H027 e.g. customers.note: a line break and a tab inside the value
  report: haunt-report.json
ghostrow: 3 tests failed with edge-case data. See haunt-report.json.

That was 33 test runs in total: the baseline, the all-cases run, and 31 narrowing runs. The seven column groups were run first, three of them failed a tracked test (email, display name, note), and halving took over from there. The report lists the four display-name cases: accents, emoji, right-to-left text and the line break. It lists three email cases (plus tag, capitals, apostrophe) and one note case, the line break. The other 28 cases, including the 64-character email, the dates and the quantities, did not break anything. The command exits with code 3, so CI fails; no failures means exit 0.

haunt-report.json holds the same result in full: each failure with its case ids and columns, every run with its case list, exit code and duration, and the cases themselves with their values.

What limits it

A run budget. Narrowing stops after 40 runs. If the budget runs out, the groups still in the queue are blamed as a whole instead of being narrowed further, so you get a longer list, not a wrong one. We hit this on our first attempt: the report said 42 runs. The cause was our own test. Edge rows that copy a base row get new unique values, so an email column in a copied row looks like h<case kind><number>.<original>, with a hyphen in it. Our first regex did not allow hyphens, so cases about dates and note lengths were flagged by the email test. We loosened the regex, and the run above is the second. If a test is strict about the format of a unique key, expect that.

Test names come from parsing output. Haunt reads failing test names from Vitest, Jest, Mocha, Node's TAP output, pytest, Go, RSpec, PHPUnit, Cargo and JUnit-style lines. If it recognises none and the command failed, the whole command counts as one failing test, named after its exit code. We saw this: with Node 24's default reporter and no flag, the same suite produced one failure called test command (exit 1). It still narrowed the failure to eight cases across email, display name and note columns, but it could not tell us which test needed which. If your runner has a TAP or per-test line reporter, use it.

Tests that depend on each other. Group runs assume a test fails because of the cases in the run. A flaky test can send the search down the wrong branch, and the baseline only catches tests that fail every time.

The attribution is plain search over test runs, not a model. Haunt runs your command in your shell, on your machine, with a 20-minute limit per run.

All posts