{"record":{"id":"477cef446de0fb99","repo":"vitest-dev/vitest","slug":"vitest-mocks-require-a-url-string","errorCode":null,"errorMessage":"[vitest] Mocks require a url string.","messagePattern":"\\[vitest\\] Mocks require a url string\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/registry.ts","lineNumber":99,"sourceCode":"      else if (event.type === 'redirect') {\n        const module = RedirectedModule.fromJSON(event)\n        this.add(module)\n        return module\n      }\n      else if (event.type === 'manual') {\n        throw new Error(`Cannot set serialized manual mock. Define a factory function manually with \\`ManualMockedModule.fromJSON()\\`.`)\n      }\n      else {\n        throw new Error(`Unknown mock type: ${(event as any).type}`)\n      }\n    }\n\n    if (typeof raw !== 'string') {\n      throw new TypeError('[vitest] Mocks require a raw string.')\n    }\n\n    if (typeof url !== 'string') {\n      throw new TypeError('[vitest] Mocks require a url string.')\n    }\n\n    if (typeof id !== 'string') {\n      throw new TypeError('[vitest] Mocks require an id string.')\n    }\n\n    if (type === 'manual') {\n      if (typeof factoryOrRedirect !== 'function') {\n        throw new TypeError('[vitest] Manual mocks require a factory function.')\n      }\n      const mock = new ManualMockedModule(raw, id, url, factoryOrRedirect)\n      this.add(mock)\n      return mock\n    }\n    else if (type === 'automock' || type === 'autospy') {\n      const mock = type === 'automock'\n        ? new AutomockedModule(raw, id, url)\n        : new AutospiedModule(raw, id, url)","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/registry.ts#L81-L117","documentation":"Thrown as a `TypeError` by `MockerRegistry.register` (multi-argument overload) when the `url` argument is not a string. The `url` is the resolved module URL used as the registry key (and for browser-mode request matching); it must be a string so the mock can be stored and looked up by URL.","triggerScenarios":"Calling `register(type, raw, id, url, ...)` with `url` set to `undefined` or a non-string — typically because the RPC resolution did not return a `resolvedUrl` or the caller passed arguments out of order (note the overload order is `type, raw, id, url`, not `type, raw, url, id`).","commonSituations":"Misordered positional arguments in a forked runner; an RPC response missing `resolvedUrl`; passing the URL field under a different name.","solutions":["Verify the argument order matches the overload signature `(type, raw, id, url, factoryOrRedirect)`.","Ensure the RPC `resolveMock`/`resolveId` response includes a string `resolvedUrl` and that it is forwarded as `url`.","Add a defensive default (e.g., fall back to `id`) only if you understand why the URL is missing."],"exampleFix":"// before — arguments swapped (url and id)\nregistry.register('automock', raw, url, id)\n// after — correct order: type, raw, id, url\nregistry.register('automock', raw, id, url)","handlingStrategy":"validation","validationCode":"// Validate url is a string before calling register.\nfunction registerSafe(registry: MockerRegistry, type: any, raw: string, id: string, url: unknown, extra?: any) {\n  if (typeof url !== 'string') {\n    throw new TypeError('url must be a string')\n  }\n  return registry.register(type, raw, id, url, extra)\n}","typeGuard":"function isUrlString(value: unknown): value is string {\n  return typeof value === 'string'\n}","tryCatchPattern":null,"preventionTips":["Ensure RPC resolveMock returns a string resolvedUrl and forward it as url.","Respect the (type, raw, id, url) argument order.","Add a regression test that swaps url/id to catch ordering bugs."],"tags":["registry","mock-registry","internal-api","validation","typeerror"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}