Reference catalog of the eight flake patterns - async/timing, test ordering, shared parallel state, resource leaks, network, locator drift, environment variance, randomness - with detection heuristics, remediation per pattern, and the concrete code-level fixes: replacing fixed sleeps with framework auto-waits, isolating state in beforeEach fixtures, per-worker DB schemas via workerIndex, try/finally teardown, mocking network + clock at the boundary, stable role-based locators, TZ pinning, and RNG seeding. Use when triaging an unknown flake to identify the category before bisecting, or when a classified flake needs the specific code change to apply.
98
91%
Does it follow best practices?
Impact
99%
1.07xAverage score across 10 eval scenarios
Passed
No findings from the security scan
test/repository.test.js was green for months. We added a sixth test last
week and the suite went red - but not on the new test. The failure was
Error: connection pool exhausted (max 5)and it landed on whatever test happened to be sixth in the file. We moved the new test up to position two out of curiosity and the failure moved with the slot, not with the test: the test that had shifted into sixth place failed instead. Every test passes on its own.
We now have seven tests and two failures. The proposals on the table are to
raise the pool's max to 50, and to split the file into two files of four
tests each. Neither feels right - production runs with a max of 5 and we
would like the tests to be honest about that.
src/pool.js and src/orderRepository.js are production code and are not in
scope for this change.
test/repository.test.js so all seven tests pass in one
node --test run with the pool still capped at 5, and so that adding an
eighth, ninth, or twentieth test to the file cannot bring the failure
back.pool.stats().inUse === 0, checked in the test file itself.src/. Keep all seven tests and their
assertions.pool-notes.md: why the failure tracked the position in the file
rather than the test, why every test passes on its own, and a verdict on
the two proposals above.Run node --test before you finish; it must pass.
Extract the following files before beginning.
=============== FILE: package.json =============== { "name": "orders-repository", "version": "3.7.1", "private": true, "scripts": { "test": "node --test" } }
=============== FILE: src/pool.js =============== 'use strict';
function createPool({ max = 5 } = {}) { const free = []; let created = 0; let inUse = 0;
function acquire() {
if (free.length === 0 && created >= max) {
throw new Error(connection pool exhausted (max ${max}));
}
const conn = free.pop() || { id: (created += 1), open: true };
inUse += 1;
return conn;
}
function release(conn) { if (!conn) { return; } inUse -= 1; free.push(conn); }
return { acquire, release, stats: () => ({ inUse, created, max }) }; }
module.exports = { createPool };
=============== FILE: src/orderRepository.js =============== 'use strict';
const table = new Map();
function assertOpen(conn) { if (!conn || !conn.open) { throw new Error('connection is not open'); } }
function insertOrder(conn, order) { assertOpen(conn); table.set(order.id, { ...order }); return order.id; }
function findOrder(conn, id) { assertOpen(conn); return table.get(id) || null; }
function deleteOrder(conn, id) { assertOpen(conn); return table.delete(id); }
function countOrders(conn) { assertOpen(conn); return table.size; }
function clearTable() { table.clear(); }
module.exports = { insertOrder, findOrder, deleteOrder, countOrders, clearTable };
=============== FILE: test/repository.test.js =============== 'use strict';
const { test, beforeEach } = require('node:test'); const assert = require('node:assert/strict'); const { createPool } = require('../src/pool'); const repo = require('../src/orderRepository'); const { insertOrder, findOrder, deleteOrder, countOrders, clearTable } = repo;
const pool = createPool({ max: 5 });
beforeEach(() => { clearTable(); });
test('an inserted order can be found', () => { const conn = pool.acquire(); insertOrder(conn, { id: 'ord-1', total: 4200 });
assert.deepEqual(findOrder(conn, 'ord-1'), { id: 'ord-1', total: 4200 }); });
test('an unknown order is not found', () => { const conn = pool.acquire();
assert.equal(findOrder(conn, 'ord-nope'), null); });
test('a deleted order is gone', () => { const conn = pool.acquire(); insertOrder(conn, { id: 'ord-2', total: 900 });
assert.equal(deleteOrder(conn, 'ord-2'), true); assert.equal(findOrder(conn, 'ord-2'), null); });
test('the table counts what was inserted', () => { const conn = pool.acquire(); insertOrder(conn, { id: 'ord-3', total: 100 }); insertOrder(conn, { id: 'ord-4', total: 200 });
assert.equal(countOrders(conn), 2); });
test('inserting the same id twice replaces the row', () => { const conn = pool.acquire(); insertOrder(conn, { id: 'ord-5', total: 100 }); insertOrder(conn, { id: 'ord-5', total: 300 });
assert.equal(countOrders(conn), 1); assert.equal(findOrder(conn, 'ord-5').total, 300); });
test('deleting an unknown order reports nothing deleted', () => { const conn = pool.acquire();
assert.equal(deleteOrder(conn, 'ord-nope'), false); });
test('an empty table counts zero', () => { const conn = pool.acquire();
assert.equal(countOrders(conn), 0); });