Skip to content

feat(browser): mock modules through an import map - #11534

Merged
sheremet-va merged 9 commits into
mainfrom
feat/browser-import-map-mocks
Oct 19, 2026
Merged

sheremet-va merged 9 commits into
mainfrom
feat/browser-import-map-mocks

Conversation

@sheremet-va

@sheremet-va sheremet-va commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

vi.mock registers an import map rule in the tester document that points the module URL at its mocked version (automock, the __mocks__ file, or a factory shim whose export names are parsed from the original module), so providers never intercept requests and the HTTP cache stays on. The orchestrator probes whether the browser accepts an import map after a module has loaded; browser.importMapMocks forces it on or off. Firefox needs dom.multiple_import_maps.enabled; the Playwright provider sets it. A rejected mock registration now fails the import instead of being dropped.

Measured with https://github.com/ubugeeei-reproductions/js-test-runner-competition (Chromium, 9 workers, 500 files, 962 modules, 1,883 mocks per suite, wall time, median of 3 interleaved rounds):

Suite Vitest 5.0.3 This change, importMapMocks: false This change, default
nomock 31.7 s 29.9 s 26.6 s
mock 56.4 s 57.7 s 27.2 s
mixed 44.0 s 51.3 s 27.1 s

Known limits: vi.unmock and vi.doUnmock cannot remove a rule (this is already the case), a factory cannot add exports the original does not declare (similar to viteModuleRunner: false), and a module imported before vi.mock fails with an explicit error (which is a good thing).

Fixes #11493
Fixes #8246

@netlify

netlify Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit 7db72e9
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6acfaa7a0d2926490e205d28
😎 Deploy Preview https://deploy-preview-11534--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@sheremet-va
sheremet-va marked this pull request as ready for review October 9, 2026 13:46
@sheremet-va sheremet-va added this to the 5.1.0 milestone Oct 10, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Browser Mode: vi.mock factory that omits an export fails with "does not provide an export named" vi.mock() doesn't work if module is already imported

1 participant