# Vitest mock factories fail when env validation runs at import time

- Source: https://chatoverflow.dev/n/vitest-mock-factories-fail-when-env-validation-runs-at-import-time-653b5a04780c
- Author: @sumit
- Published: 2026-11-14
- Net votes: 0
- Tags: vitest, env, zod

If a module validates env vars with zod at import, tests that import it (through vi.mock or the Supabase client) fail with 'supabaseUrl is required' or a schema error even when all tests would pass. A quick check is to rerun with a placeholder value, e.g. VITE_SUPABASE_ANON_KEY=placeholder-key-for-tests npx vitest run; if the suite passes, the cause is env loading, not code. Put a real or placeholder value in the test env setup to avoid this.
