vitest-dev/vitest · error · TypeError

vi.unmock() expects a string path, but received a

Error message

vi.unmock() expects a string path, but received a ${typeof path}

What it means

`vi.unmock(path)` removes a previously registered mock and requires a string path to identify the module. Passing a non-string triggers a `TypeError` (with `typeof path`) before the unmock is queued.

Solutions

  1. Pass the same string path used in `vi.mock`: `vi.unmock('./db')`.
  2. Confirm the variable is a string when path is dynamic.
  3. Distinguish from `vi.doUnmock` which also takes a string but is not hoisted.

Example fix

// before
vi.unmock(dbModule)

// after
vi.unmock('./db')
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof path !== 'string') {
  throw new TypeError(`vi.unmock requires a string path, got ${typeof path}`)
}

Type guard

const isPathString = (v: unknown): v is string => typeof v === 'string'

Prevention

When it happens

Trigger: Calling `vi.unmock(importedModule)`, `vi.unmock(42)`, or any non-string value as the path argument.

Common situations: Passing the imported namespace object instead of the path string; refactor that changed the argument; mismatched mock/unmock argument types in a setup file.

Related errors


AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11). Data as JSON: /api/errors/2c229f575a556f5c. Report an issue: GitHub.

Appendix: source

Thrown at packages/mocker/src/browser/hints.ts:86

      const importer = getImporter('mock')
      _mocker().queueMock(
        path,
        importer,
        typeof factory === 'function'
          ? () =>
              factory(() =>
                _mocker().importActual(
                  path,
                  importer,
                ),
              )
          : factory,
      )
    },

    unmock(path: string | Promise<unknown>): void {
      if (typeof path !== 'string') {
        throw new TypeError(
          `vi.unmock() expects a string path, but received a ${typeof path}`,
        )
      }
      _mocker().queueUnmock(path, getImporter('unmock'))
    },

    doMock(path: string | Promise<unknown>, factory?: ModuleMockOptions | ModuleMockFactoryWithHelper): void {
      if (typeof path !== 'string') {
        throw new TypeError(
          `vi.doMock() expects a string path, but received a ${typeof path}`,
        )
      }
      const importer = getImporter('doMock')
      _mocker().queueMock(
        path,
        importer,
        typeof factory === 'function'
          ? () =>

View on GitHub (pinned to 1fa9837ec2)