Test suites often run without a full set of environment variables. A unit test for a utility function does not needDocumentation Index
Fetch the complete documentation index at: https://next-safe-env.dev/llms.txt
Use this file to discover all available pages before exploring further.
DATABASE_URL or JWT_SECRET to be present, but next-safe-env would ordinarily crash the process if those required fields are missing. The skipValidation option lets you bypass field-level validation so your tests can import env without needing every variable populated.
Using skipValidation
PassskipValidation: process.env.NODE_ENV === 'test' to opt out of validation whenever your test runner sets NODE_ENV=test:
What skipValidation does
WhenskipValidation is true:
beforeValidate(the adapter’s prefix-enforcement hook) is skipped entirely.- Field-level validation is skipped - required fields will not throw if they are
undefined, and coercions such asnum()andbool()are not applied. afterValidatestill runs - the adapter’s key-stripping logic (for example, removing server vars in a browser context) is applied as normal.- The raw string values from
runtimeEnvare returned in the typed shape with no coercion. A field typed asnumbermay hold a string at runtime when validation is skipped.
Using a separate .env.test file with Next.js
Next.js supports environment-specific.env files out of the box. Create a .env.test file at the root of your project with only the variables your tests actually need:
.env.test when NODE_ENV is test, so your test suite picks up these values without any extra configuration. Variables not listed in .env.test will be undefined, which is safe when skipValidation is enabled.
