vitest icon indicating copy to clipboard operation
vitest copied to clipboard

Mixing browser and non-browser projects in vitest workspace causes error

Open cuberoot opened this issue 1 year ago • 2 comments

Describe the bug

I have encountered a bug in vite/vitest that occurs when I have a vitest.workspace.ts that references at least two vitest.config.ts project files that use the node environment and one vitest.config.ts project file that executes tests in a browser.

When I run vitest I get the following error:

 DEV  v1.6.0 /Users/mitaylor/Temp/VitestRaceBug
      [browser] Browser runner started at http://localhost:5173/

 ✓ |test1| src/bubblesort.test.ts (5)
 ✓ |test3| src/insertionsort.test.ts (3)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
TypeError: Cannot read properties of undefined (reading 'imports')
 ❯ node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:52698:47
 ❯ process.processTicksAndRejections node:internal/process/task_queues:95:5

I debugged a little and the issue seems to occur in runOptimizeDeps. The build outputs have relative paths instead of normalized paths. When I remove the project that has browser tests in it, then the outputs in runOptimizeDeps have normalized paths relative to the root of the project. They don't contain any .. paths.

Screenshot 2024-05-16 at 6 44 22 PM

Reproduction

I have managed to create a minimal project that demonstrates the problem. It is available here: https://github.com/cuberoot/vitest-race-issue

System Info

System:
    OS: macOS 14.4.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 453.30 MB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.16.0/bin/yarn
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
    bun: 1.1.4 - ~/.bun/bin/bun
  Browsers:
    Chrome: 125.0.6422.60
    Chrome Canary: 126.0.6478.0
    Firefox Nightly: 127.0a1
    Safari: 17.4.1
    Safari Technology Preview: 17.4
  npmPackages:
    @vitest/browser: ^1.6.0 => 1.6.0
    vite: ^5.2.11 => 5.2.11
    vitest: ^1.6.0 => 1.6.0


### Used Package Manager

npm

### Validations

- [X] Follow our [Code of Conduct](https://github.com/vitest-dev/vitest/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guidelines](https://github.com/vitest-dev/vitest/blob/main/CONTRIBUTING.md).
- [X] Read the [docs](https://vitest.dev/guide/).
- [X] Check that there isn't [already an issue](https://github.com/vitest-dev/vitest/issues) that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/vitest-dev/vitest/discussions) or join our [Discord Chat Server](https://chat.vitest.dev).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.

cuberoot avatar May 16 '24 07:05 cuberoot

As a workaround, you can run just the browser-based tests by themselves. That will cache the necessary optimized dependencies in node_modules/.vite. After that, you can run all of your tests together and they will complete.

cuberoot avatar May 16 '24 07:05 cuberoot

Thank you for the reproduction! I also noticed the same error in our tests from time to time, but did not have time to investigate,

sheremet-va avatar May 16 '24 08:05 sheremet-va

This is not reproducible anymore. We rewrote how the Browser Mode works in 2.0, so it probably fixed it.

sheremet-va avatar Oct 02 '24 12:10 sheremet-va