test-mode
test-mode
One flag, for the whole project, for faking side effects locally. Not one flag per feature: NEWSLETTER_DRY_RUN, SKIP_EMAILS, STRIPE_FAKE and friends multiply, get forgotten, and nobody remembers which ones a fresh checkout needs.
The convention
-
One variable:
TEST_MODE=1, set in.env.local(which must be gitignored — check). Nothing else. -
One helper reads it, and every gated feature imports the helper rather than reading the env directly. Put it where the project keeps shared server code, e.g.
src/lib/test-mode.ts:// Development only: production can never fake a side effect, whatever its env says. export const testMode = process.env.NODE_ENV === 'development' && process.env.TEST_MODE === '1'; -
Dev only. The
NODE_ENV === 'development'check is the point — a strayTEST_MODEin a production environment must do nothing. A consequence worth knowing: a local production build (next build && next start,vite preview) won't honour it either. -
Server side. Side effects happen on the server, so the helper is server-only. Don't expose it to the client bundle (no
NEXT_PUBLIC_/VITE_prefix).