You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Browser mode: vi.mock of a non-JS module (svg, json, ?query assets) is ignored with import map mocks (5.1.0) #11952
Since 5.1.0 serves browser mocks through an import map (#11534), vi.mock of any module that Vite imports with an explicit ?import query is silently ignored: the real module loads and no error is reported. That is every import that is not a JS or CSS request: .svg, .png, .json, ./icon.svg?component, etc.
resolveMockImportMap (packages/browser/src/node/mocks.ts) builds the key from module.url (/icon.svg?component, /plain.svg, /data.json), adding only v= and t=. Vite's import analysis emits /icon.svg?import&component, /plain.svg?import, /data.json?import (isExplicitImportRequired → injectQuery(url, 'import'), which puts import first), so the key never matches. With browser.importMapMocks: false the same test passes.
Suggested fix: apply Vite's rule when building the key. If the URL is not a JS/CSS request, inject import the way injectQuery does (/a.svg?component → /a.svg?import&component). Writing the mock specifier as ./icon.svg?import&component makes the SVG cases pass, which confirms the key mismatch. JSON then resolves to undefined, so the manual-mock shim for JSON may need a look too.
resolveMockImportMap now adds import to the key the way Vite does (only for non-JS/CSS URLs, at the front of the query), and the new mocked-non-js.test.ts fixture mocks an ?raw svg, a plain svg and a json file. It fails without the change and passes with it in the importMapMocks: true run of specs/mocking.test.ts.
Happy to open a PR if this gets the approved label.
Activity
mohammad-naji7 commentedon Nov 12, 2026
I have a fix with a regression test ready: main...mohammad-naji7:vitest:fix/browser-import-map-mock-non-js
resolveMockImportMapnow addsimportto the key the way Vite does (only for non-JS/CSS URLs, at the front of the query), and the newmocked-non-js.test.tsfixture mocks an?rawsvg, a plain svg and a json file. It fails without the change and passes with it in theimportMapMocks: truerun ofspecs/mocking.test.ts.Happy to open a PR if this gets the
approvedlabel.