vitest-dev/vitest · error · TypeError

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

Error message

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

What it means

Thrown at packages/mocker/src/browser/hints.ts:116-119 as a TypeError when vi.doUnmock() is called with a non-string first argument. vi.doUnmock is the non-hoisted counterpart of vi.unmock and needs a string specifier to remove a mock from the registry at call time.

Source

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

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

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

    async importActual<T = unknown>(path: string): Promise<T> {
      return _mocker().importActual<T>(
        path,
        getImporter('importActual'),
      )
    },

    async importMock<T>(path: string): Promise<MaybeMockedDeep<T>> {
      return _mocker().importMock(path, getImporter('importMock'))
    },
  }
}

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Pass a string literal module path: vi.doUnmock('./logger').
  2. Validate any dynamic path is a string before calling.

Example fix

// before
vi.doUnmock(import('./logger'))

// after
vi.doUnmock('./logger')
Defensive patterns

Strategy: type-guard

Validate before calling

function doUnmockIfString(path: unknown) {
  if (typeof path !== 'string') {
    throw new TypeError(`vi.doUnmock requires a string path, got ${typeof path}`)
  }
  vi.doUnmock(path)
}

Type guard

function isModulePath(path: unknown): path is string {
  return typeof path === 'string' && path.length > 0
}

Prevention

When it happens

Trigger: Calling vi.doUnmock(undefined), vi.doUnmock(someVar) where someVar is not a string, or vi.doUnmock(import('./x')) that escaped transform rewriting.

Common situations: Refactoring unmock calls to doUnmock and dropping the path; passing a computed value that is not a string.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/0ede60c5c0032e65.json. Report an issue: GitHub.