{"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":"validation","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/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/mocker/src/registry.ts#L81-L117","documentation":"The positional register overload requires `url` — the resolved absolute file URL of the mocked module (e.g. `file:///abs/path/mod.js`) — to be a string. The registry indexes mocks by `url`, so a non-string value cannot be stored.","triggerScenarios":"Calling `registry.register('automock', raw, id, undefined)` or passing a non-string `url`, e.g. a URL object.","commonSituations":"Passing a `URL` instance instead of `.toString()`/`.href`; wrappers that resolve paths asynchronously and forward `undefined`; misordered args.","solutions":["Pass a string URL (call `.href` or `String(url)` if you hold a `URL` object).","Confirm argument order: `(type, raw, id, url, factoryOrRedirect)`.","Resolve the specifier to a file URL before calling register."],"exampleFix":"// before\nregistry.register('automock', raw, id, new URL('file:///x'))\n\n// after\nregistry.register('automock', raw, id, 'file:///abs/path/mod.js')","handlingStrategy":"validation","validationCode":"function toUrlString(url: unknown): string {\n  if (typeof url === 'string') return url\n  if (url instanceof URL) return url.href\n  throw new TypeError(`register(): 'url' must be a string or URL, got ${typeof url}`)\n}\n\nregistry.register(type, raw, id, toUrlString(url), extra)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always resolve specifiers to `file://` strings before registering.","If you hold a `URL`, call `.href` explicitly.","Centralize URL normalization in one helper used by all mock-registration calls."],"tags":["mocking","registry","validation"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}