vitest-dev / vscode

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

Unable to run tests as vitest bootstrapping fails #388

Closed moltco closed 1 month ago

moltco commented 1 month ago

Describe the bug

Vitest VS extension appears not to be loading the default .env file when discovering tests. If I run yarn vitest the tests are executed as expected. If I specify manually the .env file then the VS Code extension seems to be behaving as exepcted. This issue only appears to be present in the VS extension.

The issue is similar to https://github.com/vitest-dev/vitest/issues/2117 and https://stackoverflow.com/questions/61038365/node-js-not-recognizing-env-file If I manually add .env file to vitest.config.ts then the VS Code Extension works as expected.

Otherwise the discovery fails with "Config param DB_ID_HOST not defined" and the vitest output window shows:

[Worker] Collecting tests due to file changes: model/Org.test.t
[INFO 9:11:50 AM] [TEST] ❌ Config param DB_ID_HOST not defined.

The DB_ID_HOST obviously exists, Jest and Playwright have no issues with it and the code where the exception is raised works well.

The real issue here is: as the test dicovery has failed, I can't run any tests at the same level which renders the extension useless.

I would expect that the default .env file (ie in the root as per vitest.config.ts) is picked up by the extension and the vitest in general unless the user does not explicitly change this via the config file or otherwise. Alternatively the vitest quick guide needs to highlight that you must specify env for tests and extension to work.

image

Reproduction

Vitest.config.ts in case it helps reproduce

export default defineConfig({
  test: {
    name: "my test",
    root: "./src",
    },
});

The failing line of code (ie the one that throws error) is in the db layer initialized in a pretty standard way


const cn = {
  host: nodeutil.getSafeConfig("DB_ID_HOST"),
...
}

the getSafeConfig does very little - throws Error if process.env[paramName] === null || === undefined

Output

See screenshot and description above

Version

vitest/1.6.0 win32-x64 node-v20.13.1 Extension v0.10.7

Validations

github-actions[bot] commented 1 month ago

Hello @moltco. Please provide a minimal reproduction using a GitHub repository or StackBlitz (you can also use examples). Issues marked with needs reproduction will be closed if they have no activity within 3 days.