connor4312 / nodejs-testing

VS Code integration for node:test native tests
MIT License
48 stars 7 forks source link

Tests run in their local directory so `.env` file in the root won't be loaded #8

Closed andokai closed 7 months ago

andokai commented 1 year ago

Hi, first, thanks for creating this very helpful extension!

I'm using node:test to run tests for an API built with Fastify. I have some environment variables set in a .env file. Fastify uses Dotenv to read in environment variables from any .env file in the current working directory. So running node --test test/**/*.test.js works because that is run at the root. However, the same tests fail in this extension. I debugged the test and I could see that process.cwd() was telling me that the test was running inside the folder where the test was. To verify my assumption I copied the .env file into that folder and the test passed.

I'd rather not have to copy my .env file into every folder where I have a test or not set it in code, just for the sake of this extension, so could it be updated so that it runs tests from the project's root directory?

Thanks!

daKmoR commented 1 year ago

with https://github.com/connor4312/nodejs-testing/pull/12 you then should be able to pass on a custom node script to your tests which would be able to load a global .env

e.g. create a new file withEnv.js

import * as dotenv from 'dotenv';

dotenv.config({
  path: new URL('./.env', import.meta.url),
});

and add 2 settings to nodejs-testing.nodejsParameters

  1. --import
  2. ${workspaceFolder}/withEnv.js

then Fastify will have those settings already available in the env (e.g. Fastify will not need to load this info by itself)

connor4312 commented 1 year ago

I think it's still be preferable to run with the cwd set to the closest of either the workspace folder or nearest package.json-containing directory. PR welcome, code pointer

https://github.com/connor4312/nodejs-testing/blob/118f3303d1b1ea7e070865a84380972a3b18e464/src/runner-worker.ts#L121

uwinkelvos commented 7 months ago

While that would obviously be a good to implement as also for other features some (imho not so well designed) tests expect cwd to be the workspace folder, for the problem at hand #30 would also provide a solution.

connor4312 commented 7 months ago

30 is merged

9600bauds commented 2 months ago

Heya, sorry, I'm a bit late to this convo. I've been having the exact same problem as described at the top of this issue. I see now that a new "Env file" setting was added to the extension settings, but I'm at a loss on how to use it. Any directory I define there, including ${workspaceFolder}, results in this VScode error: "EISDIR: illegal operation on a directory, read". Any tips? Thanks in advance.

uwinkelvos commented 2 months ago

Did you set the absolute path to the env file, not just the directory where a .env file is located in?

9600bauds commented 2 months ago

Ah, my mistake, it does run perfectly if i set "${workspaceFolder}/.env". Thank you for the amazingly quick response!