vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
12.64k stars 1.13k forks source link

`process.argv` mismatching between `1.6.0` and `2.0.x` #6203

Open nazarhussain opened 1 month ago

nazarhussain commented 1 month ago

Describe the bug

The default value for process.argv does not match between above mentioned versions, when run with the browsers mode.

If we use vite-plugin-node-polyfills to pollyfil the process it had different weird behavior.

The vite-plugin-node-polyfills version is same in both cases, so the shims does not change at all.

Reproduction

https://stackblitz.com/edit/vitest-2-env-argv https://stackblitz.com/edit/vitest-1-env-argv

System Info

System:
    OS: macOS 14.5
    CPU: (11) arm64 Apple M3 Pro
    Memory: 200.19 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.4.0 - ~/.asdf/installs/nodejs/22.4.0/bin/node
    Yarn: 1.22.22 - ~/.asdf/installs/nodejs/22.4.0/bin/yarn
    npm: 10.8.1 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Brave Browser: 126.1.67.134
    Safari: 17.5

Used Package Manager

yarn

Validations

sheremet-va commented 1 month ago

This probably happens because we want to statically replace testing-library's env vars:

https://github.com/vitest-dev/vitest/blob/883f3482745772ccf24087246f4f7af7414ae233/packages/browser/src/node/plugin.ts#L339

But looks like adding process.* creates an empty process object globally:

https://github.com/vitejs/vite/blob/b240a8347e7b62bee9d2212625732bb0d8c78633/packages/vite/src/client/env.ts#L17

Not sure how to fix this yet.

nazarhussain commented 1 month ago

In our project a deep dependency is looking for process.argv and that is causing to fail the upgrade to vitest 2.0.x.

Until it's fixed, is there a workaround you could think of?

sheremet-va commented 1 month ago

Until it's fixed, is there a workaround you could think of?

I don't think this can be fixed soon on Vite side, but you can define process.argv in defines:

export default defineConfig({
  defines: {
    'process.argv': '[]',
  }
})
sheremet-va commented 1 month ago

Looks like the previous fix breaks config bundling (esbuild hangs) with headless: false

nazarhussain commented 1 month ago

Until it's fixed, is there a workaround you could think of?

I don't think this can be fixed soon on Vite side, but you can define process.argv in defines:

export default defineConfig({
  defines: {
    'process.argv': '[]',
  }
})

In my case I had to also add one other attribute to process.

{
          define: {
            "process.argv": "[]",
            "process.nextTick": "function noop(){}",
          },
}