{"record":{"id":"9982aa5d75f20470","repo":"vitest-dev/vitest","slug":"vitest-vi-mock-raw-factory-unknown","errorCode":null,"errorMessage":"[vitest] vi.mock(\"${raw}\", factory?: () => unknown) is not returning an object. Did you mean to return an object with a \"default\" key?","messagePattern":"\\[vitest\\] vi\\.mock\\(\"(.+?)\", factory\\?: \\(\\) => unknown\\) is not returning an object\\. Did you mean to return an object with a \"default\" key\\?","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/registry.ts","lineNumber":327,"sourceCode":"      id: this.id,\n      raw: this.raw,\n    }\n  }\n}\n\nfunction createHelpfulError(cause: Error) {\n  const error = new Error(\n    '[vitest] There was an error when mocking a module. '\n    + 'If you are using \"vi.mock\" factory, make sure there are no top level variables inside, since this call is hoisted to top of the file. '\n    + 'Read more: https://vitest.dev/api/vi.html#vi-mock',\n  )\n  error.cause = cause\n  return error\n}\n\nfunction assertValidExports(raw: string, exports: any) {\n  if (exports === null || typeof exports !== 'object' || Array.isArray(exports)) {\n    throw new TypeError(\n      `[vitest] vi.mock(\"${raw}\", factory?: () => unknown) is not returning an object. Did you mean to return an object with a \"default\" key?`,\n    )\n  }\n}\n\nexport interface ManualMockedModuleSerialized {\n  type: 'manual'\n  url: string\n  id: string\n  raw: string\n}\n","sourceCodeStart":309,"sourceCodeEnd":339,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/registry.ts#L309-L339","documentation":"Thrown by assertValidExports when a manual mock factory's return value is null, a non-object, or an array. ManualMockedModule.resolve() runs the factory and asserts the exports are a plain object, because the module system expects a namespace object (with a 'default' key for default exports). Returning a primitive or forgetting to return is the most common cause.","triggerScenarios":"A vi.mock factory that returns undefined (missing return statement), returns a string/number, returns null, or returns an array; an async factory whose promise resolves to a non-object. Triggered at module load time when the mocked module is first imported.","commonSituations":"Forgetting `return` in the factory arrow function; returning a class instance or function instead of wrapping it in { default: ... }; mock factory that conditionally returns; ESM default-export mock that returned the value directly instead of { default: value }.","solutions":[],"exampleFix":"// before\nvi.mock('./logger', () => console.log)\n// after\nvi.mock('./logger', () => ({ default: console.log, log: console.log }))","handlingStrategy":"validation","validationCode":"// Inside a vi.mock factory, validate before returning\nvi.mock('./mod', () => {\n  const exports = { default: 'x' }\n  if (exports === null || typeof exports !== 'object' || Array.isArray(exports)) {\n    throw new TypeError('factory must return a plain object')\n  }\n  return exports\n})","typeGuard":"function isPlainObjectExports(v: unknown): v is Record<string, unknown> { return v !== null && typeof v === 'object' && !Array.isArray(v) }","tryCatchPattern":null,"preventionTips":["Always return an object literal from the factory","Wrap single values in { default: value } for ESM default exports","Avoid implicit undefined returns in factory arrow functions"],"tags":["vitest","mocker","vi-mock","factory","exports"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}