Closed silvenon closed 5 months ago
NODE_ENV
is set to test
by Vitest.
Not while running tests via the extension, because my tests that rely on this are failing (because of that). Running the same tests via the Vitest CLI works.
In fact, logging process.env.NODE_ENV
in beforeAll
ends up being development
to me 😮
This is then also the source of my WebSocket issue because my tests are createing Vite dev server middleware in case of development
.
Here's the repro: https://github.com/silvenon/repro/tree/vitest-extension-node-env
It looks like NODE_ENV ??= 'test'
happens in startVitest
API which is a wrapper of createVitest
API used by vscode extension:
export async function startVitest(...): Promise<Vitest | undefined> {
process.env.TEST = 'true'
process.env.VITEST = 'true'
process.env.NODE_ENV ??= 'test'
...
const ctx = await createVitest(mode, options, viteOverrides, vitestOptions)
When NODE_ENV
is empty, if I remember correctly, Vite injects "development" by default, so probably that's what's happening currently.
For the time being, I think using vitest.nodeEnv
should work (at least for test code, but maybe not if it's globalSetup etc...).
// .vscode/settings.json
{
"vitest.nodeEnv": {
"NODE_ENV": "test"
}
}
I'm not sure where the fix should belong to. Whether adding the same process.env....
routine to vscode extension worker or createVitest
should have this routine instead of startVitest
wrapper.
I'm thinking we can fix createVitest
side on Vitest, but maybe we should do both since users might not upgrade Vitest.
Describe the bug
I had two problems when testing my Express server with
supertest
with the pre-release of the extension, this was one of them. I'm aware ofprocess.env.VITEST_VSCODE
, but that's just a workaround for this problem,NODE_ENV
should still be set totest
just like Vitest does.I wasn't able to figure out the other problem, I keep getting "WebSocket server error: Port is already in use" in my logs and all of my server tests fail through the extension with 404 status instead of the expected one, even though I made sure not to start my server. But I suppose without a repro it's hard to tell what could've gone wrong.
Reproduction
https://github.com/silvenon/repro/tree/vitest-extension-node-env
For WebSocket I'll file a separate issue.
System Info
Used Package Manager
npm
Validations