vitest icon indicating copy to clipboard operation
vitest copied to clipboard

HMR overlay captures test clicks and cannot be disabled

Open steedems opened this issue 1 year ago • 4 comments

Describe the bug

When using Vitest browser mode, tests re-run before the error HMR overlay is removed. This causes test interactions (like clicks) to be intercepted by the overlay, resulting in unintended behaviour such as opening editor links from the stack trace.

Additionally, the server.hmr.overlay: false configuration option in vitest.browser.config.ts is ignored and does not disable the overlay as expected.

Reproduction

Checkout vitest-hmr-overlay-troubleshoot.

git clone https://github.com/steedems/vitest-hmr-overlay-troubleshoot.git
  1. Run pnpm test:browser - test should pass
  2. Introduce a syntax error in HelloWorld.test.tsx (e.g., remove a quote from the test name)
  3. HMR overlay appears showing the syntax error and stack trace ✓
  4. Fix the syntax error
  5. The test re-runs before the HMR overlay is removed. Therefore, clicks fired by the test itself underneath the overlay are captured by the overlay and links of the stack trace are opened in the editor ✗

Additional issue:

Image

System Info

System:
    OS: macOS 15.6.1
    CPU: (10) arm64 Apple M2 Pro
    Memory: 91.61 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.17.0 - ~/.nvm/versions/node/v20.17.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.17.0/bin/yarn
    npm: 10.8.2 - ~/.nvm/versions/node/v20.17.0/bin/npm
    pnpm: 9.12.1 - ~/Library/pnpm/pnpm
    Watchman: 2024.09.23.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 139.0.7258.157
    Safari: 18.6
  npmPackages:
    @vitejs/plugin-react: ^5.0.0 => 5.0.2
    @vitest/browser: ^3.2.4 => 3.2.4
    playwright: ^1.55.0 => 1.55.0
    vite: ^7.1.2 => 7.1.5
    vitest: ^3.2.4 => 3.2.4
    vitest-browser-react: ^1.0.1 => 1.0.1

Used Package Manager

pnpm

Validations

steedems avatar Sep 08 '25 11:09 steedems

Confirming this on [email protected] / @vitest/[email protected] / [email protected], with a second trigger and what I think is the reason the config option is ignored.

Why server.hmr.overlay: false has no effect. Vitest sets server.hmr: false (several sites, e.g. vitest/dist/chunks/cli-api.*.js:1255 and init.*.js:28). Vite then does, in vite/dist/node/chunks/config.js:27411-27416:

let hmrConfig = config.server.hmr;
hmrConfig = isObject(hmrConfig) ? hmrConfig : void 0;   // false is not an object → undefined
const overlay = hmrConfig?.overlay !== false;            // undefined?.overlay → undefined !== false → true

So hmr: false collapses the whole object before overlay is read, and overlay is unconditionally true. The option isn't being dropped somewhere — it's structurally unreachable in browser mode, because the thing that would carry it has already been replaced. Any fix probably has to thread the overlay preference separately from server.hmr.

A trigger that doesn't involve HMR, a re-run, or a user mistake. In the original report the overlay is left over from a syntax error while the suite re-runs. Mine appears mid-run in a suite that is otherwise passing: a Vite plugin (vite-imagetools) throws synchronously inside dev-server middleware when it can't resolve an asset id, and Vite renders the overlay for it. No source edit, no HMR event, nothing for a user to fix. Same outcome — the overlay takes clicks the test aimed at the page.

That made it unusually hard to attribute. Symptoms were four unrelated test files failing intermittently, roughly 1 run in 3 to 13, on assertions with no obvious common factor: dialog.open === false after clicking a menu button, <details>.open === false after clicking its <summary>, document.activeElement still <body>, a form-submit callback never firing. Three separate hypotheses died before the failure screenshots in __screenshots__/ showed the overlay.

Worth adding for anyone debugging this: the overlay is in the orchestrator document, not the test iframe. userEvent drives a real pointer at top-level coordinates, so it hits the overlay — but every check from inside the iframe says the click should have worked. I hid the overlay in the iframe, confirmed display: none, confirmed elementFromPoint returned the intended element, and the click still failed. It only made sense after looking at window.parent.document.

Workaround, if it helps others until the option works — a browser-project setup file that hides it in both documents:

// setupFiles: ['./suppressViteErrorOverlay.ts']
const hide = (doc: Document) => {
  const style = doc.createElement('style');
  style.textContent = 'vite-error-overlay { display: none !important; }';
  doc.head.append(style);
};
hide(document);
if (window.parent !== window) hide(window.parent.document);

Note this only suppresses the symptom — whatever failed to serve is still failing, just no longer stealing input. In my case one of the underlying asset failures still breaks a test in the file that owns it, which is the correct place for it to break.

nickwilson159 avatar Aug 28 '26 16:08 nickwilson159

Confirmed on main. Vitest sets server.hmr: false for every project (packages/vitest/src/node/plugins/config.ts), and Vite's client replacer does hmrConfig = isObject(hmrConfig) ? hmrConfig : undefined before reading overlay. As a result, server.hmr.overlaycan never reach the client in browser mode, and the overlay is always on.

It affects both documents. The tester gets /@vite/client via transformIndexHtml; the orchestrator gets it indirectly, because Vite's import analysis prepends import { injectQuery } from "/@vite/client" to modules with dynamic imports, which the orchestrator chunks have. Loading the client is what registers the overlay.

Vite's client only defines vite-error-overlay if customElements.get('vite-error-overlay') is empty, and createErrorOverlay instantiates whatever is registered. So Vitest could register a no-op element (with a close() method) before /@vite/client runs, in the orchestrator's injector script and as a head-prepended tag in the tester, and the overlay would never render, with no CSS or internals involved. Since Vitest already reports these errors itself, I'd make that the default rather than add an option.

Happy to open a PR for that if you agree with the direction.

nomadical avatar Oct 01 '26 13:10 nomadical

I’ve prepared a fix. The change is in esm-client-injector.js.

Because this script runs before anything else in both the orchestrator and tester iframe, it registers a no-op vite-error-overlay custom element. When Vite’s client sees the element is already registered, it doesn't create its own overlay, so nothing blocks the page. Vitest continues to report the errors itself.

The change also includes a browser test using a fixture whose plugin throws during load. The test fails before the fix and passes afterward.

Would you be open to a PR using this approach, or would you prefer to address it through configuration with server.hmr: false?

The link points to GitHub’s compare page. Although that page includes a “Create pull request” button, no PR opens until someone clicks it, so the 24-hour auto-close shouldn't trigger.

nomadical avatar Oct 08 '26 06:10 nomadical

Friendly ping on this one. The branch still merges cleanly into current main. If you'd rather fix it on the Vite side (letting server.hmr: false also turn off the overlay), I can open an issue there instead. Just let me know which way you prefer.

nomadical avatar Oct 28 '26 17:10 nomadical