vitest-dev / vscode

VS Code extension for Vitest
https://vitest.dev/vscode
MIT License
729 stars 79 forks source link

`envalid` erroring out when continuous run is enabled and on first re-run #406

Closed snasirca closed 1 month ago

snasirca commented 1 month ago

Describe the bug

When running Vitest in continuous watch mode using the VS Code Vitest extension, the process unexpectedly exits with an error related to envalid. This issue does not occur when running Vitest directly via the command line (vitest dev). The error indicates that process.exit is called unexpectedly due to missing environment variables, even though they are correctly loaded using dotenv.config() in a setup file.

Reproduction

  1. Use the https://github.com/edwinhern/express-typescript-2024 project as a starting point by cloning it locally
  2. Run npm install in the directory
  3. Add a FOO environment variable to the envConfig.ts file:
    diff --git a/src/common/utils/envConfig.ts b/src/common/utils/envConfig.ts
    index 73d6a1e..570911f 100644
    --- a/src/common/utils/envConfig.ts
    +++ b/src/common/utils/envConfig.ts
    @@ -10,4 +10,5 @@ export const env = cleanEnv(process.env, {
       CORS_ORIGIN: str({ devDefault: testOnly('http://localhost:3000') }),
       COMMON_RATE_LIMIT_MAX_REQUESTS: num({ devDefault: testOnly(1000) }),
       COMMON_RATE_LIMIT_WINDOW_MS: num({ devDefault: testOnly(1000) }),
    +  FOO: str(),
     });
  4. Run Vitest in watch mode using the VS Code extension:
    • Open the Testing sidebar in VS Code.
    • Click on the "Continuous Run" icon
      • CleanShot 2024-06-03 at 22 55 48
  5. Make a change to a test file like src/api/user/__tests__/userRouter.test.ts and save it

Output

image

================================
 Missing environment variables:
    FOO: undefined
================================

 Exiting with error code 1

~~~~~~~~~~~~ AND ~~~~~~~~~~~~

Error: process.exit unexpectedly called with "1"
    at process.exit (file:///path/to/node_modules/vitest/dist/vendor/execute.js:401:11)
    at defaultReporter (/path/to/node_modules/envalid/src/reporter.ts:81:13)
    at getSanitizedEnv (/path/to/node_modules/envalid/src/core.ts:104:3)
    at Proxy.cleanEnv (/path/to/node_modules/envalid/src/envalid.ts:18:34)
    at /path/to/project/src/common/utils/env-config.ts:7:20
    at VitestExecutor.runModule (file:///path/to/node_modules/vite-node/dist/client.mjs:362:5)
    at VitestExecutor.directRequest (file:///path/to/node_modules/vite-node/dist/client.mjs:346:5)
    at VitestExecutor.cachedRequest (file:///path/to/node_modules/vite-node/dist/client.mjs:189:14)
    at VitestExecutor.dependencyRequest (file:///path/to/node_modules/vite-node/dist/client.mjs:233:12)
    at /path/to/project/src/api/area/area-repository.ts:2:31

Version

0.10.7

Validations