{"record":{"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":"exception","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/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/browser/hints.ts#L77-L113","documentation":"`vi.doMock(path, factory)` is the non-hoisted sibling of `vi.mock` and likewise requires a string path. The browser hints layer throws a `TypeError` (reporting `typeof path`) for any non-string first argument before queueing the mock.","triggerScenarios":"Calling `vi.doMock(moduleObject)`, `vi.doMock(Promise)`, or any non-string; using `vi.doMock` with an imported binding rather than its path.","commonSituations":"Switching from `vi.mock` to `vi.doMock` for runtime-specified modules but passing an object; dynamic-import helpers that yield a module namespace instead of a path string.","solutions":["Pass the module path as a string: `vi.doMock('./db', factory)`.","Resolve the path string before calling when it is computed at runtime.","Use `vi.mocked` for typing an imported mock, not `vi.doMock`."],"exampleFix":"// before\nvi.doMock(await import('./db'))\n\n// after\nvi.doMock('./db', () => ({ query: vi.fn() }))","handlingStrategy":"type-guard","validationCode":"if (typeof path !== 'string') {\n  throw new TypeError(`vi.doMock requires a string path, got ${typeof path}`)\n}","typeGuard":"const isPathString = (v: unknown): v is string => typeof v === 'string'","tryCatchPattern":null,"preventionTips":["Pass a string path to vi.doMock even when computed at runtime.","Use vi.mocked for typing imported mocks.","Resolve dynamic imports to a path string before calling."],"tags":["mocker","vi-domock","type-error","browser"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}