{"record":{"id":"3797218f7f3fd750","repo":"vitest-dev/vitest","slug":"vi-mock-expects-a-string-path-but-received-a-379721","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/vitest/src/integrations/vi.ts","lineNumber":648,"sourceCode":"            catch (error) {\n              if (error instanceof Error && !error.stack?.includes('__VITEST_HELPER__')) {\n                copyStackTrace(error, stackTraceError)\n              }\n              throw error\n            }\n          })()\n        }\n        return result\n      } as any\n    },\n    hoisted<T>(factory: () => T): T {\n      assertTypes(factory, '\"vi.hoisted\" factory', ['function'])\n      return factory()\n    },\n\n    mock(path: string | Promise<unknown>, factory?: MockOptions | MockFactoryWithHelper) {\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                  _mocker().getMockContext().callstack,\n                ),\n              )\n          : factory,\n      )","sourceCodeStart":630,"sourceCodeEnd":666,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/integrations/vi.ts#L630-L666","documentation":"Thrown by `vi.mock(path, factory)` when `path` is not a string. `vi.mock` is hoisted by the Vitest transformer and must receive a module specifier (string literal in source); passing a non-string (Promise, number, object, dynamic value) breaks hoisting and module resolution, so a TypeError is raised immediately.","triggerScenarios":"Calling `vi.mock(someVariable)` where the variable is not a string; passing a Promise or imported module object; using a dynamic/conditional path; accidentally passing the module namespace object instead of its specifier.","commonSituations":"Trying to mock a path computed at runtime; passing an imported binding instead of a path string; refactoring that replaces the literal with a variable; misunderstanding that `vi.mock` must be statically analyzable.","solutions":["Pass a string literal path: `vi.mock('./myModule', () => {...})`.","If the path must be dynamic, use `vi.doMock` inside the test body (still requires a string) — but prefer a static literal for hoisting.","Ensure you are not passing the imported module object (e.g. `import * as M` then `vi.mock(M)`)."],"exampleFix":"// before\nconst modPath = './myModule'\nvi.mock(modPath)\n\n// after\nvi.mock('./myModule')","handlingStrategy":"type-guard","validationCode":"function mockSafe(path, factory) {\n  if (typeof path !== 'string') throw new TypeError('vi.mock requires a string path')\n  return vi.mock(path, factory)\n}","typeGuard":"function isModulePath(v): v is string {\n  return typeof v === 'string' && v.length > 0\n}","tryCatchPattern":null,"preventionTips":["Always pass a string literal specifier to vi.mock for hoisting.","Never pass an imported module object or Promise as the path.","Use vi.doMock for runtime-controlled mocking (still string-only)."],"tags":["vi-mock","hoisting","type-error","module-path"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}