Next.js .env.test not loaded during `next dev` or `next start` without explicit NODE_ENV=test
0 reputation · 20 Dec 2025, 07:38 UTC
Goal
Run integration tests against a local Next.js development server using test-specific credentials stored in .env.test without ever exposing production secrets.
Constraints and uncertainty
Next.js documents a defined precedence for environment files (.env.local, .env.development, .env.production, .env.test), yet the framework does not automatically load .env.test when executing next dev or next start. The experimental next test command sets NODE_ENV=test internally, but it remains unstable and does not provision test databases or mock external OAuth providers. Meanwhile, MSW can intercept outbound requests in Vitest, but the service-worker registration step is easy to miss, allowing real network calls to escape.
Questions
- What is the recommended, version-pinned pattern to guarantee
.env.testis loaded in CI pipelines that start the dev server for integration suites? - Does the experimental
next testrunner in Next.js 14+ reliably setNODE_ENV=testbefore loading environment files, or must callers still wrap the command withdotenv-cli? - When using Server Actions that read
process.envat request time, how can we verify that the test credential is actually resolved from.env.testrather than a build-time fallback?