The Environment Variable That Only Breaks in Staging

"It works on my machine" has a specific, common variant that's worth naming on its own: it works on localhost, it works in production, and it breaks specifically in staging — or the reverse, works everywhere except one environment nobody tests against as carefully as the other two. This isn't random bad luck. It's almost always a sign that something environment-specific was assumed to be the same everywhere, and it wasn't.
The usual suspects
CORS. A backend's allowed-origins list is a common one: localhost:3000 is allowed for local dev, the production domain is allowed because that's what actually matters, and staging's oddly-named subdomain (staging-app-x7f2.internal.example.com, or whatever your infra generated) was never added to the list because nobody thought about it as a "real" origin worth configuring deliberately. The request from staging fails a CORS preflight, and the error a developer sees in the browser console — a vague network failure — doesn't obviously point at "the allowed-origins config is incomplete."
Absolute vs. relative URLs. Code that hardcodes http://localhost:8000/api obviously breaks outside local dev, but the more insidious version is a base URL computed from an environment variable that's set correctly in two environments and quietly wrong (or missing, silently falling back to a bad default) in the third.
Cookies and domain scoping. A cookie set with a domain or SameSite policy tuned for production's real domain structure can behave differently on staging's different domain structure — session cookies that work in production but silently don't get sent on staging is a specific, recurring version of this.
Feature flags or config defaulting differently per environment, sometimes deliberately (a flag is off in staging because a feature isn't ready) and sometimes by accident (a config value has a hardcoded production default with no staging-specific override, and nobody noticed because staging traffic is low enough that the resulting bug barely gets seen).
Why this is genuinely hard to catch with normal testing
Unit and integration tests overwhelmingly run in one place — usually something closer to a clean local or CI environment than any of your actual deployed environments — which means they systematically don't exercise the exact category of bug that only shows up when environment-specific configuration diverges. A test suite that passes locally and in CI tells you the code is correct given the configuration it was tested with. It says nothing about whether staging's actual configuration matches what the code assumes.
Making the difference visible instead of assumed
The practical fix isn't "test in every environment for everything," which doesn't scale — it's making environment-specific configuration differences visible and testable in isolation, rather than only discoverable by deploying and seeing what breaks. Standing up a mock of a dependency at each environment's actual expected origin/domain, and confirming a request from that specific origin succeeds against your CORS configuration, catches the missing-origin case in minutes instead of during a staging deploy that someone happens to notice failed. The same goes for confirming your actual client code resolves the right base URL per environment — pointing a build meant for "staging" at a mock running at a staging-shaped URL and confirming it actually reaches it, rather than trusting that an environment variable was set the way you assume it was.
None of this replaces testing against the real thing eventually. It just moves the moment you'd discover an environment-specific misconfiguration from "a deploy to staging, days after the code was written" to "a five-minute check while you're still looking at the change."
Xerat02
Building MockBase.
More from the blog

Why Your Staging Environment Needs Its Own Fake Third Parties
Pointing staging at a payment or SMS provider's sandbox feels like the responsible choice — and it quietly imports every one of that sandbox's own failure modes into your test environment.

PUT vs PATCH: The Difference That Breaks More APIs Than It Should
"Replace the whole thing" and "change one field" sound like a minor implementation detail — until a client sends a partial PUT, silently wipes out data it never meant to touch, and nobody notices until support does.