Repository navigation
Vitest browser tests hang on navigation or form submission #8583
Description
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:
- Clone the reproduction repository: https://github.com/bteng22/vitest-browser-hangs-repro
- Follow the instructions in the README to install dependencies and run the browser tests.
- 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.1Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsHas plan
Activity
sheremet-va commentedon Sep 19, 2025
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.
sheremet-va commentedon Sep 19, 2025
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.
bteng22 commentedon Sep 19, 2025
thanks @sheremet-va! Showing an error would be a great start 👍
silvenon commentedon Jan 6, 2026
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.sheremet-va commentedon Jan 7, 2026
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