{"id":"59e6771cdcd2b4e1","repo":"vitest-dev/vitest","slug":"vitest-mocks-require-a-raw-string","errorCode":null,"errorMessage":"[vitest] Mocks require a raw string.","messagePattern":"\\[vitest\\] Mocks require a raw string\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/registry.ts","lineNumber":95,"sourceCode":"        const module = AutospiedModule.fromJSON(event)\n        this.add(module)\n        return module\n      }\n      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    }","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/mocker/src/registry.ts#L77-L113","documentation":"The positional overload `register(type, raw, id, url, factoryOrRedirect)` requires the second argument `raw` — the original module specifier exactly as written in user code (e.g. `'./config'`) — to be a string. Passing `undefined` or a non-string is treated as a programmer error.","triggerScenarios":"Calling `registry.register('automock', undefined, id, url)` (raw omitted) or passing a number/object as the specifier.","commonSituations":"Programmatic mock construction in wrappers; misordered arguments; refactor that dropped the specifier.","solutions":["Pass the original specifier string as the second argument.","Verify argument order against the overload signature: `(type, raw, id, url, factoryOrRedirect)`.","Enable strict TypeScript checks so missing string args are caught at compile time."],"exampleFix":"// before\nregistry.register('automock', undefined, id, url)\n\n// after\nregistry.register('automock', './config', id, url)","handlingStrategy":"validation","validationCode":"function registerTyped(registry: any, type: string, raw: unknown, id: string, url: string, extra?: unknown) {\n  if (typeof raw !== 'string') {\n    throw new TypeError(`register('${type}'): 'raw' must be a string specifier, got ${typeof raw}`)\n  }\n  registry.register(type, raw, id, url, extra)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Type the positional register arguments strictly in wrapper code.","Keep the original specifier (e.g. './config') alongside the resolved id/url so it is always available.","Enable `strict` TypeScript so non-string args fail to compile."],"tags":["mocking","registry","validation"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}