{"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/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/integrations/vi.ts#L630-L666","documentation":"`vi.mock(path, factory)` queues a module mock identified by a string module path. The guard rejects any non-string `path` with a `TypeError` reporting the received type, because the mocker keys mocks by resolved string path and cannot queue a mock for a non-string identifier. The signature accepts `string | Promise<unknown>` for type-level flexibility but enforces string at runtime.","triggerScenarios":"Calling `vi.mock(123)`, `vi.mock(someObject)`, `vi.mock(import('./mod'))` (passing a promise/module namespace), or `vi.mock(undefined)`.","commonSituations":"Trying to pass a dynamic import expression as the path, or constructing the path from a variable that is accidentally undefined/non-string.","solutions":["Pass the module specifier as a literal string: `vi.mock('./api')`.","If the path is dynamic, ensure the variable is a string before calling (e.g. `vi.mock(String(name))`).","Use `vi.doMock` with a runtime string if the path is not statically known."],"exampleFix":"// before\nvi.mock(import('./api'))\n// after\nvi.mock('./api')","handlingStrategy":"type-guard","validationCode":"function mockPath(path: unknown) {\n  if (typeof path !== 'string') throw new TypeError(`vi.mock expects string, got ${typeof path}`)\n  vi.mock(path)\n}","typeGuard":"function isModulePath(v: unknown): v is string { return typeof v === 'string' && v.length > 0 }","tryCatchPattern":null,"preventionTips":["Always pass a string literal module specifier to `vi.mock`.","Use `vi.doMock` for runtime-computed string paths."],"tags":["mock","vi-mock","type-error","validation"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}