Vitest says an export is missing from a module you mocked
Your error
[vitest] No "fetchUser" export is defined on the "./api" mock. Did you forget to return it from "vi.mock"?2 more lines it can showFix it fast
Most common cause, 1 of 2The factory returns only the functions the test cares about
Make it a partial mock.
vi.mock(import('./api'), async (importOriginal) => {
const actual = await importOriginal()
return {
...actual,
fetchUser: vi.fn(),
}
})Short answer
Vitest throws No "name" export is defined on the "path" mock when code reads an export missing from the object the vi.mock factory returns. The factory replaces the whole module: spread the real module from importOriginal and override only what the test needs, and add a default key for a default export. Applies to Vitest 5.
Which cause do you have?
Note the export name and the path the message quotes, then print the vi.mock call for that path. The quoted name is missing from the object the factory returns.
grep -rn -A8 "vi.mock(['\"]./api['\"]" src/Then find the line you see in this list and open its fix:
- 1The factory returns only the functions the test cares about
The missing name is a named export that the test never touches, such as a helper the component under test also imports - 2The module has a default export and the factory has no default key
The message names "default", and the code under test uses import config from './config' or a default-exported class
What does the error look like?
[vitest] No "fetchUser" export is defined on the "./api" mock. Did you forget to return it from "vi.mock"?[vitest] No "default" export is defined on the "./config" mock. Did you forget to return it from "vi.mock"?If you need to partially mock a module, you can use "importOriginal" helper inside:
Cause 1 of 2
The factory returns only the functions the test cares about
You seeThe missing name is a named export that the test never touches, such as a helper the component under test also imports
Check
When code accesses a property that the mocked module object does not have, Vitest's module mocker throws this error instead of returning undefined. A factory that returns only fetchUser therefore breaks the first file that also imports formatUser from the same module.
grep -rn "from './api'" src/Fix
Make it a partial mock. Pass the module as an import() so types follow it, await importOriginal, spread the real exports and replace only the ones the test controls.
vi.mock(import('./api'), async (importOriginal) => {
const actual = await importOriginal()
return {
...actual,
fetchUser: vi.fn(),
}
})Verify
The suite runs, the overridden function is a mock, and other exports behave as in production.
npx vitest run src/api.test.tsConfirmed by raw.githubusercontent.com
Cause 2 of 2
The module has a default export and the factory has no default key
You seeThe message names "default", and the code under test uses import config from './config' or a default-exported class
Check
With ES modules a default export is just an export named default. Vitest's vi.mock reference warns that mocking such a module needs a default key in the object the factory returns; returning the value directly, as Jest examples written for CommonJS do, leaves no default key, so the mocker throws as soon as the default import is read.
grep -n "export default" src/config.tsFix
Return an object with a default property holding the mocked value, plus any named exports the code reads.
vi.mock('./config', () => {
return {
default: { apiUrl: 'http://localhost', retry: vi.fn() },
loadConfig: vi.fn(),
}
})Verify
The default import in the code under test now receives the mocked object.
npx vitest runConfirmed by vitest.dev
How do I stop it coming back?
- Start every module mock from importOriginal and a spread, and remove it only when the test must prove that no real code runs.
- Write vi.mock(import('./path'), ...) so TypeScript checks the factory's return type against the real module.
- Keep shared module mocks in a __mocks__ file next to the module so every export is defined in one place.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.