{"record":{"id":"563053fb6311d434","repo":"vitest-dev/vitest","slug":"vi-mock-expects-a-string-path-but-received-a","errorCode":null,"errorMessage":"vi.mock() expects a string path, but received a ${typeof path}","messagePattern":"vi\\.mock\\(\\) expects a string path, but received a (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/browser/hints.ts","lineNumber":64,"sourceCode":"              )\n            },\n          },\n        )\n  }\n\n  return {\n    hoisted<T>(factory: () => T): T {\n      if (typeof factory !== 'function') {\n        throw new TypeError(\n          `vi.hoisted() expects a function, but received a ${typeof factory}`,\n        )\n      }\n      return factory()\n    },\n\n    mock(path: string | Promise<unknown>, factory?: ModuleMockOptions | ModuleMockFactoryWithHelper): void {\n      if (typeof path !== 'string') {\n        throw new TypeError(\n          `vi.mock() expects a string path, but received a ${typeof path}`,\n        )\n      }\n      const importer = getImporter('mock')\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":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/browser/hints.ts#L46-L82","documentation":"`vi.mock(path, factory)` registers a module mock and requires `path` to be a string so the resolver can locate the module. The browser hints layer throws a `TypeError` (with the runtime `typeof`) when a non-string is passed, before queuing the mock.","triggerScenarios":"Passing a module object, a Promise, a number, or an imported binding object to `vi.mock` as the first argument; passing a variable that was reassigned away from a string.","commonSituations":"Confusing `vi.mock` (string path) with `vi.mocked` (wrapped import); passing the imported module itself instead of its path; copy-paste where the path was replaced by a value.","solutions":["Pass the module path as a string literal: `vi.mock('./db')`.","Use `vi.mocked(importedFn)` to type-check an already-imported mock, not `vi.mock`.","If the path is dynamic, ensure the variable is a string at call time."],"exampleFix":"// before\nimport * as db from './db'\nvi.mock(db)\n\n// after\nvi.mock('./db')","handlingStrategy":"type-guard","validationCode":"if (typeof path !== 'string') {\n  throw new TypeError(`vi.mock requires a string path, got ${typeof path}`)\n}","typeGuard":"const isPathString = (v: unknown): v is string => typeof v === 'string'","tryCatchPattern":null,"preventionTips":["Pass the module path as a string literal to vi.mock.","Use vi.mocked() to type-check an imported mock, not vi.mock().","Avoid passing imported namespace objects as the path."],"tags":["mocker","vi-mock","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"}