Your tests that make network requests are flaky
Key takeaway: Keep every network call out of the test suite that blocks merges, and run real network tests on their own.
If a test makes a network request, it’s flaky. “The network is reliable” is one of the Fallacies of Distributed Computing. Networks drop packets, slow down, and fail DNS, and a test that depends on one inherits all of that.
That’s why these tests pass locally and flake in CI. Your laptop talks to itself fast. CI runners talk to real services over real networks.
What it really damages is trust. Once the first instinct on a red build is “just re-run it,” the team stops investigating failures and stops believing green. I’ve seen teams with 200-test suites where 15 are “known flaky,” and those 15 train everyone to ignore failures. Then a real bug gets lost in the noise.
Keep the network out of the suite that gates merges. The easiest seam is the HTTP call. Pass it in, with the real fetch as the default:
// exchange-rate.ts
type Fetch = (url: string) => Promise<Response>;
export async function usdTo(currency: string, fetchFn: Fetch = fetch) {
const res = await fetchFn("https://rates.example.com/latest?base=USD");
if (!res.ok) throw new Error(`rates request failed: ${res.status}`);
const body = (await res.json()) as { rates: Record<string, number> };
const rate = body.rates[currency];
if (rate === undefined) throw new Error(`no rate for ${currency}`);
return rate;
}
Now a test hands it a fake that returns a rate, or a 503, and gets the same answer in milliseconds every time. The outage test is one a real network would only run by accident.
Don’t mock everything, though. If everything is mocked, the test isn’t checking much. To check that your fakes still match reality, run contract tests against the real service on a schedule, outside the merge gate.
Save real network calls for a few smoke tests that run separately and never gate a deploy on their own. This matters even more when coding agents depend on the same feedback loop. A flaky test wastes a person’s patience, and at scale it wastes an agent’s time and tokens.