Skip to content

Vitest browser tests hang on navigation or form submission #8583

Closed

Description

@bteng22

Describe the bug

When running Vitest in browser mode, tests that trigger navigation or execute the default function of an onSubmit handler will hang indefinitely. This appears to happen because the test browser attempts to navigate away potentially causing the test runner to lose connection with the underlying node process. There doesn't seem to be clear documentation or guidance about how to handle or prevent these hangs in browser-enabled tests.

Reproduction

External reproduction: https://github.com/bteng22/vitest-browser-hangs-repro

Steps to reproduce:

  1. Clone the reproduction repository: https://github.com/bteng22/vitest-browser-hangs-repro
  2. Follow the instructions in the README to install dependencies and run the browser tests.
  3. Observe that when a navigation is triggered (via e.g. a form's default onSubmit), the test hangs and never completes.

Actual behaviour:

Vitest browser tests hang forever when navigation or a default onSubmit handler is triggered. The test never completes and requires manual intervention to stop. This includes programmatic navigations as well with window.location.assign or window.history.pushState

Expected behaviour:

Vitest browser tests should not hang or lose connection with the node process when navigation or form submission occurs. There should be clear documentation or built-in handling for these situations, or at least guidance on best practices for preventing such hangs in browser-enabled tests?

System Info

System:
    OS: macOS 15.6.1
    CPU: (16) arm64 Apple M3 Max
    Memory: 322.59 MB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 23.7.0 - ~/.nvm/versions/node/v23.7.0/bin/node
    npm: 10.9.2 - ~/.nvm/versions/node/v23.7.0/bin/npm
  Browsers:
    Chrome: 140.0.7339.133
    Edge: 140.0.3485.66
    Safari: 18.6
  npmPackages:
    @vitejs/plugin-react: ^4.7.0 => 4.7.0 
    @vitejs/plugin-react-swc: ^3.11.0 => 3.11.0 
    @vitest/browser: 3.2.4 => 3.2.4 
    @vitest/ui: 3.2.4 => 3.2.4 
    playwright: 1.54.2 => 1.54.2 
    vite: 7.0.6 => 7.0.6 
    vitest: 3.2.4 => 3.2.4 
    vitest-browser-react: 1.0.1 => 1.0.1

Used Package Manager

npm

Validations

Activity

  1. added theissue type on Sep 16, 2025
  2. sheremet-va commented on Sep 19, 2025

    @sheremet-va
    Member

    Vitest browser tests should not hang or lose connection with the node process when navigation or form submission occurs. There should be clear documentation or built-in handling for these situations, or at least guidance on best practices for preventing such hangs in browser-enabled tests?

    Agree that there should be some kind of guidance. I would expect people to prevent the event. I am not sure if we can automatically resolve this. Cypress, for example, endlessly restarts the test file in this case.

  3. sheremet-va commented on Sep 19, 2025

    @sheremet-va
    Member

    Found a way to throw an error in this case: #8601

    Until we can figure out a better solution, let's show an error at least.

  4. bteng22 commented on Sep 19, 2025

    @bteng22
    Author

    thanks @sheremet-va! Showing an error would be a great start 👍

  5. moved this to P2 - 3 in Team Boardon Nov 27, 2025
  6. silvenon commented on Jan 6, 2026

    @silvenon
    SponsorContributor

    In my case my component called window.location.reload() and the runner exhibited the same behavior, despite using the latest version of Vitest. It took me a long time to figure it out… is there something different about reloading that makes it circumvent your fix and avoid the throw? And could you please also catch the reload scenario and throw? It would be hugely helpful 🙏

    A hanging runner is just so paralyzing, I don't know whether it's waiting for something to be true, or running an action, or it lost connection, and then I have no idea where to look until I remember the navigation scenario 😓

    But then I'm so thankful that I can just surround my navigation logic with if (import.meta.env.MODE !== 'test') 😌 neat little trick.

  7. sheremet-va commented on Jan 7, 2026

    @sheremet-va
    Member

    is there something different about reloading that makes it circumvent your fix and avoid the throw?

    The current handling only kicks in if you change the URL, it's ignored when it's reloaded in a loop or a new page is open

  8. moved this from P2 - 3 to Has plan in Team Boardon Jan 23, 2026
  9. added
    p3-minor-bugAn edge case that only affects very specific usage (priority)
    and removed
    p2-to-be-discussedEnhancement under consideration (priority)
    on Jan 23, 2026
  10. locked and limited conversation to collaborators on Feb 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: browserIssues and PRs related to the browser runnerp3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions