{"id":"ea85c89477210b39","repo":"vitest-dev/vitest","slug":"vi-domock-expects-a-string-path-but-received-a","errorCode":null,"errorMessage":"vi.doMock() expects a string path, but received a ${typeof path}","messagePattern":"vi\\.doMock\\(\\) expects a string path, but received a (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/browser/hints.ts","lineNumber":95,"sourceCode":"                  importer,\n                ),\n              )\n          : factory,\n      )\n    },\n\n    unmock(path: string | Promise<unknown>): void {\n      if (typeof path !== 'string') {\n        throw new TypeError(\n          `vi.unmock() expects a string path, but received a ${typeof path}`,\n        )\n      }\n      _mocker().queueUnmock(path, getImporter('unmock'))\n    },\n\n    doMock(path: string | Promise<unknown>, factory?: ModuleMockOptions | ModuleMockFactoryWithHelper): void {\n      if (typeof path !== 'string') {\n        throw new TypeError(\n          `vi.doMock() expects a string path, but received a ${typeof path}`,\n        )\n      }\n      const importer = getImporter('doMock')\n      _mocker().queueMock(\n        path,\n        importer,\n        typeof factory === 'function'\n          ? () =>\n              factory(() =>\n                _mocker().importActual(\n                  path,\n                  importer,\n                ),\n              )\n          : factory,\n      )\n    },","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/mocker/src/browser/hints.ts#L77-L113","documentation":"Thrown at packages/mocker/src/browser/hints.ts:94-98 as a TypeError when vi.doMock() is called with a non-string first argument. vi.doMock is the non-hoisted variant of vi.mock and likewise requires a string module specifier to resolve and register the mock at call time.","triggerScenarios":"Calling vi.doMock(undefined), vi.doMock(someVar) where someVar is not a string, or vi.doMock(import('./x')) that escaped the transform rewrite.","commonSituations":"Using vi.doMock inside an async function with a dynamically computed path that resolved to undefined; refactoring from vi.mock to vi.doMock and forgetting the path argument.","solutions":["Pass a string literal module path: vi.doMock('./logger').","If computing the path dynamically, validate it is a string before calling doMock."],"exampleFix":"// before\nvi.doMock(import('./logger'))\n\n// after\nvi.doMock('./logger')","handlingStrategy":"type-guard","validationCode":"function doMockIfString(path: unknown, factory?: () => any) {\n  if (typeof path !== 'string') {\n    throw new TypeError(`vi.doMock requires a string path, got ${typeof path}`)\n  }\n  vi.doMock(path, factory)\n}","typeGuard":"function isModulePath(path: unknown): path is string {\n  return typeof path === 'string' && path.length > 0\n}","tryCatchPattern":null,"preventionTips":["Pass a string literal module specifier to vi.doMock.","Validate dynamically computed paths are strings before calling."],"tags":["vi-domock","type-validation","typeerror","module-path"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}