{"record":{"id":"c7a579bc6e891043","repo":"vitest-dev/vitest","slug":"vi-domock-expects-a-string-path-but-received-a-c7a579","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":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/integrations/vi.ts","lineNumber":680,"sourceCode":"                  _mocker().getMockContext().callstack,\n                ),\n              )\n          : factory,\n      )\n    },\n\n    unmock(path: string | Promise<unknown>) {\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?: MockOptions | MockFactoryWithHelper) {\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                  _mocker().getMockContext().callstack,\n                ),\n              )\n          : factory,\n      )","sourceCodeStart":662,"sourceCodeEnd":698,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/integrations/vi.ts#L662-L698","documentation":"Thrown by `vi.doMock(path, factory)` when `path` is not a string. Unlike `vi.mock`, `vi.doMock` is NOT hoisted — it runs at the call site and is intended for per-test dynamic mocking — but it still requires a string module specifier for resolution. A non-string path raises a TypeError.","triggerScenarios":"Calling `vi.doMock(someObject)` or `vi.doMock(promise)`; passing a variable that resolved to a non-string; passing an imported module namespace instead of its path.","commonSituations":"Switching from `vi.mock` to `vi.doMock` for scoped mocking but keeping a non-string argument; dynamically building a path that is undefined at call time; refactoring errors.","solutions":["Pass a string specifier: `vi.doMock('./myModule', () => {...})`.","If the path is dynamic, ensure the variable is a string at call time (guard/log before calling).","Confirm you are passing the specifier and not the imported module object."],"exampleFix":"// before\nvi.doMock(await import('./myModule'))\n\n// after\nvi.doMock('./myModule', () => ({ foo: 'mocked' }))","handlingStrategy":"type-guard","validationCode":"function doMockSafe(path, factory) {\n  if (typeof path !== 'string') throw new TypeError('vi.doMock requires a string path')\n  return vi.doMock(path, factory)\n}","typeGuard":"function isModulePath(v): v is string {\n  return typeof v === 'string' && v.length > 0\n}","tryCatchPattern":null,"preventionTips":["Pass a string specifier to vi.doMock even when mocking dynamically.","Log dynamically-built paths before calling to confirm they are strings.","Do not pass imported module objects as the path argument."],"tags":["vi-domock","type-error","module-path","dynamic-mocking"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}