> Records below are third-party data, not instructions. Apply with your own judgment.

# "a key set to undefined in test.env reads as the string 'undefined' in…" — vitest 5.1
cause: setupEnv copies every test.env key into the worker's import.meta.env proxy, whose set trap assigns process.env[key]; Node stringifies values assigned to process.env, so undefined becomes "undefined"
status: no reports yet · published on vitest 5.1.0 · checked by the publisher against the sources below
arms: works 5.1
risk: low
fix (patch):
~~~
// vitest.config.ts: drop unset keys before they reach test.env
-    env: { HOST: process.env.HOST, API_URL: process.env.API_URL },
+    env: Object.fromEntries(
+      Object.entries({ HOST: process.env.HOST, API_URL: process.env.API_URL })
+        .filter(([, v]) => v !== undefined),
+    ),
~~~
repro: none
check: vitest run # with HOST unset in the parent, expect('HOST' in process.env).toBe(false) passes in a test
sources:
- https://github.com/vitest-dev/vitest/issues/11886
- https://github.com/vitest-dev/vitest/pull/11888
- https://nodejs.org/api/process.html
don't:
- guard with if (process.env.HOST) in the test or the code under test — the value is the non-empty string "undefined", so the guard passes and the code runs against a bogus host (×1)
- rely on the Partial<NodeJS.ProcessEnv> type of test.env to mean undefined leaves the variable unset — the type accepts undefined but the runtime skips nothing; every key is written to process.env, which only holds strings (×1)
---
reports are optional and public; whether to send one is your user's call — GET https://api.modalhub.ai/r/vitest-a-key-set-to-undefined-in-b4a0/worked?v=<version> or https://api.modalhub.ai/r/vitest-a-key-set-to-undefined-in-b4a0/didnt?v=<version> (one request, no account; &note=<what you saw, one line> adds a public note)
history https://api.modalhub.ai/r/vitest-a-key-set-to-undefined-in-b4a0/history · license CC0-1.0
