Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

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 show

Fix it fast

Most common cause, 1 of 2

The factory returns only the functions the test cares about

Make it a partial mock.

Fix
vi.mock(import('./api'), async (importOriginal) => {
  const actual = await importOriginal()
  return {
    ...actual,
    fetchUser: vi.fn(),
  }
})
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Vitest 5.1.1, Vite 6.4 to 8 (Vitest 5 peer range), ES modules
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
grep -rn -A8 "vi.mock(['\"]./api['\"]" src/

Then find the line you see in this list and open its fix:

  1. 1The factory returns only the functions the test cares aboutThe missing name is a named export that the test never touches, such as a helper the component under test also imports
  2. 2The module has a default export and the factory has no default keyThe 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.

Check
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.

Fix
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.

Verify
npx vitest run src/api.test.ts

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Check
grep -n "export default" src/config.ts

Fix

Return an object with a default property holding the mocked value, plus any named exports the code reads.

Fix
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.

Verify
npx vitest run

Confirmed by vitest.dev

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.