Skip to content

Race condition in test runner when using "define" in the vite configuration #6976

@timothympace

Description

@timothympace

Describe the bug

I've come across what appears to be a race condition in the vitest test runner. It occurs when using the define configuration option in the vitest config and then referencing a variable defined through it in the import tree of a test file.

I'm not positive of the conditions that reproduce this, but I was able to get a minimal reproduction (linked below). Unfortunately, it can take A LOT of runs (I'm seeing within 30 minutes of repeatedly running the test command) for it to choke, hence why I believe it's race condition.

The best I can tell is that this happens when there is some non-trivial period of time spent importing/resolving/parsing modules in the import path and then referencing a variable in the define from there. The reproduction does this with an artificial delay in a shared file that all the tests import, with some random amount of wait time in it (scoped to a min/max that seems to best reproduce it)

Reproduction

See: https://github.com/timothympace/vitest-define-bug

There is info in the README, but I'll also copy paste it here:

Steps to reproduce

Run:

pnpm repro-bug

Wait for failure. It could take A LOT of runs. I've seen it take up to 30 minutes to reproduce, and it wouldn't shock me if it takes hours even.

System Info

System:
    OS: macOS 15.1.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 240.14 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.2.0 - ~/Library/Caches/fnm_multishells/22284_1732553878425/bin/node
    Yarn: 1.22.19 - ~/Library/Caches/fnm_multishells/22284_1732553878425/bin/yarn
    npm: 9.6.6 - ~/Library/Caches/fnm_multishells/22284_1732553878425/bin/npm
    pnpm: 9.5.0 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 131.0.6778.86
    Safari: 18.1.1
  npmPackages:
    vitest: ^2.1.6 => 2.1.6

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions