Make it deterministic
All labsThis suite passes, then fails, then passes. The engine runs it under a hundred seeds, varying the seeded randomness and the order in which same-instant timers fire, and reports how often it disagreed with itself. Every entry in the flake taxonomy has a deterministic fix, and the fix is almost always to make the varying thing an input rather than something the test reaches for.
goalGet the failure rate to zero across a hundred seeds — without deleting the assertions.
The module
export function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export function shuffleInto(target, items) {
for (const item of items) {
setTimeout(() => target.push(item), 0);
}
return target;
}
export function pickWinner(entrants, random) {
const draw = typeof random === "function" ? random : Math.random;
return entrants[Math.floor(draw() * entrants.length)];
}
Your tests
Failure rate across a hundred seeds
Run the suite to measure it.