Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests are being run with NODE_ENV=development #305

Closed
6 tasks done
silvenon opened this issue Mar 19, 2024 · 5 comments · Fixed by #309
Closed
6 tasks done

Tests are being run with NODE_ENV=development #305

silvenon opened this issue Mar 19, 2024 · 5 comments · Fixed by #309
Labels
p4-important Violate documented behavior or significantly improves performance (priority)

Comments

@silvenon
Copy link

silvenon commented Mar 19, 2024

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 of process.env.VITEST_VSCODE, but that's just a workaround for this problem, NODE_ENV should still be set to test 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.

Note

I just realized that the WebSocket issue is most likely due to the development value. My server file is creating Vite dev server middleware in that case, which is not intended for tests to run.

Reproduction

https://github.com/silvenon/repro/tree/vitest-extension-node-env

For WebSocket I'll file a separate issue.

System Info

System:
    OS: macOS 14.4
    CPU: (8) arm64 Apple M1 Pro
    Memory: 51.06 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.19.1 - ~/.n/bin/node
    npm: 10.2.4 - ~/.n/bin/npm
    pnpm: 8.6.10 - /opt/homebrew/bin/pnpm
    bun: 1.0.31 - ~/.bun/bin/bun
  IDEs:
    VSCode: 1.87.2 - /usr/local/bin/code
    Vim: 9.0 - /usr/bin/vim
    Xcode: /undefined - /usr/bin/xcodebuild
  Browsers:
    Chrome: 122.0.6261.129
    Edge: 122.0.2365.92
    Safari: 17.4
  npmPackages:
    @vitejs/plugin-react: ^4.2.0 => 4.2.0 
    @vitest/ui: ^1.2.2 => 1.4.0 
    vite: ^5.0.0 => 5.0.0 
    vitest: ^1.4.0 => 1.4.0

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

sheremet-va commented Mar 19, 2024

NODE_ENV is set to test by Vitest.

@silvenon
Copy link
Author

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.

@silvenon
Copy link
Author

silvenon commented Mar 19, 2024

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.

@silvenon
Copy link
Author

@silvenon silvenon changed the title Tests are not being run with NODE_ENV=test Tests are being run with NODE_ENV=development Mar 19, 2024
@sheremet-va sheremet-va added p4-important Violate documented behavior or significantly improves performance (priority) and removed pending triage labels Mar 19, 2024
@hi-ogawa
Copy link
Contributor

It looks like NODE_ENV ??= 'test' happens in startVitest API which is a wrapper of createVitest API used by vscode extension:

https://github.com/vitest-dev/vitest/blob/5c7e9ca05491aeda225ce4616f06eefcd068c0b4/packages/vitest/src/node/cli/cli-api.ts#L31-L55

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.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p4-important Violate documented behavior or significantly improves performance (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants