{"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":"validation","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/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/mocker/src/browser/hints.ts#L46-L82","documentation":"Thrown at packages/mocker/src/browser/hints.ts:63-67 as a TypeError when vi.mock() is called with a first argument that is not a string. vi.mock needs a module specifier to resolve and register the mock; the type guard rejects non-string paths before they reach the registry.","triggerScenarios":"Calling vi.mock(someVariable) where the variable is undefined/not a string; vi.mock(import('./x')) where the dynamic import was not rewritten to its source by the transform; vi.mock(123) or vi.mock({}).","commonSituations":"Passing a dynamic import expression that the hoistMocks transform failed to rewrite (hoistMocks.ts:344-371); passing a path from configuration that resolved to undefined; template strings that evaluated to non-string.","solutions":["Pass a string literal module path: vi.mock('./logger').","If using a variable, ensure it is typed and assigned a string before the call.","Avoid passing import() expressions directly; let the transformer rewrite them or use a plain string."],"exampleFix":"// before\nvi.mock(import('./logger'))\n\n// after\nvi.mock('./logger')","handlingStrategy":"type-guard","validationCode":"// Ensure the path is a string before calling vi.mock.\nfunction mockIfString(path: unknown, factory?: () => any) {\n  if (typeof path !== 'string') {\n    throw new TypeError(`vi.mock requires a string path, got ${typeof path}`)\n  }\n  vi.mock(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.mock.","Avoid passing import() expressions directly; let the transformer rewrite them or inline the string.","Type the path variable as string in helper functions."],"tags":["vi-mock","type-validation","typeerror","module-path"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}