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/profiles.test.js fails about one run in five. Two failure shapes, both
from the same file:
Expected values to be strictly deep-equal:
+ actual - expected
[
+ 'u-2',
+ 'u-1',
- 'u-1',
- 'u-2',
'u-3',
'u-4'
]
expected 'billing' to equal 'platform'The right four profiles come back every time. Nothing is ever missing and nothing is ever duplicated - two of them just trade places. The pairs that trade are always neighbours in the list.
It is worse on the CI runner than on a laptop and worse when the box is
busy, but it happens everywhere. src/profiles.js is shipped code with a
documented behaviour, and the team that owns it has said the behaviour is not
changing.
test/profiles.test.js so all three tests pass on every run.u-2 is on the platform team. Do not modify
src/profiles.js.profile-diagnosis.md: what the assertions were relying on, why it
holds most of the time, and the rule for asserting on the output of this
function in future tests.Run node --test before you finish; it must pass.
Extract the following files before beginning.
=============== FILE: package.json =============== { "name": "directory-service", "version": "2.9.0", "private": true, "scripts": { "test": "node --test" } }
=============== FILE: src/profiles.js =============== 'use strict';
const DIRECTORY = { 'u-1': { id: 'u-1', name: 'Ada', team: 'platform' }, 'u-2': { id: 'u-2', name: 'Grace', team: 'platform' }, 'u-3': { id: 'u-3', name: 'Katherine', team: 'billing' }, 'u-4': { id: 'u-4', name: 'Dorothy', team: 'billing' }, };
// Per-record lookup latency. A cold record costs an extra round trip. const BASE_LATENCY = { 'u-1': 20, 'u-2': 55, 'u-3': 90, 'u-4': 125 };
function loadProfile(id) { const cold = Math.random() < 0.06; return new Promise((resolve) => { setTimeout( () => resolve({ ...DIRECTORY[id] }), BASE_LATENCY[id] + (cold ? 50 : 0) ); }); }
// Looks the ids up concurrently. Results are collected as each lookup // finishes; the returned list is not ordered by the ids that were requested. async function loadProfiles(ids) { const found = []; await Promise.all( ids.map(async (id) => { found.push(await loadProfile(id)); }) ); return found; }
module.exports = { loadProfiles };
=============== FILE: test/profiles.test.js =============== 'use strict';
const test = require('node:test'); const assert = require('node:assert/strict'); const { loadProfiles } = require('../src/profiles');
test('every requested profile comes back', async () => { const profiles = await loadProfiles(['u-1', 'u-2', 'u-3', 'u-4']);
assert.deepEqual( profiles.map((p) => p.id), ['u-1', 'u-2', 'u-3', 'u-4'] ); });
test('a profile carries its team', async () => { const profiles = await loadProfiles(['u-2', 'u-3']);
assert.equal(profiles[0].team, 'platform'); });
test('an empty request returns nothing', async () => { const profiles = await loadProfiles([]);
assert.equal(profiles.length, 0); });