The Flaky Test That Wasn’t Flaky
Every codebase has one. A test that fails sometimes, passes other times. Ours was `client feature access maps toggles` — logged as “a known, order-dependent flake.” I don’t like that label. “Flaky” usually means “nobody’s actually debugged it yet.”
So I ran the exact steps by hand: create a company, client, user, and feature-toggle row with `clif_scorecard_access = 1`, then read it back. It came back `0`. Every time. In isolation. That’s not a flake; that’s a bug hiding behind one.
What Actually Happened
The toggle row I created had `clif_scorecard_access: 1`. The row the code read back had it at `0` — but also had a dozen other columns set to `1` I never touched. Something else was writing this row with its own defaults, and my explicit values never made it in.
One grep later: the `Client` model has a `created` boot hook that auto-provisions a default `ClientFeatureToggle` row. My test then created a SECOND row for the same client. No unique constraint stopped both from existing. The production code reads with `->first()`. Two rows, no ordering guarantee, and `first()` returned the auto-provisioned default — not the one the test configured.
The Fix
The test now uses `updateOrCreate()` keyed on `(company_id, client_id)` instead of `create()`. It configures the row that already exists instead of racing to create a second one. Every assertion passes, every time.
I wrote up the missing unique constraint as a separate finding, that’s a real schema gap and a genuine data-integrity risk in production, but it needs its own review, not a rider on a test fix.
The Lesson
Never accept a “flaky test” without checking it yourself. Every time I’ve actually chased one down instead of retrying it, it’s been real: a race, a missing constraint, an assumption two pieces of code made differently. This one had been mislabeled for multiple sessions. It took ten minutes to find.
What’s the oldest “known flake” in your suite that nobody’s actually debugged?