Closed andokai closed 7 months 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
--import
${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)
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
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.
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.
Did you set the absolute path to the env file, not just the directory where a .env file is located in?
Ah, my mistake, it does run perfectly if i set "${workspaceFolder}/.env". Thank you for the amazingly quick response!
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 runningnode --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 thatprocess.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!