{"id":"632d7a83554c3e94","repo":"vitest-dev/vitest","slug":"vi-hoisted-expects-a-function-but-received-a","errorCode":null,"errorMessage":"vi.hoisted() expects a function, but received a ${typeof factory}","messagePattern":"vi\\.hoisted\\(\\) expects a function, but received a (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/browser/hints.ts","lineNumber":55,"sourceCode":"      // @ts-expect-error injected by the plugin\n      ? globalThis[globalThisAccessor]\n      : new Proxy(\n          {} as any,\n          {\n            get(_, name) {\n              throw new Error(\n                'Vitest mocker was not initialized in this environment. '\n                + `vi.${String(name)}() is forbidden.`,\n              )\n            },\n          },\n        )\n  }\n\n  return {\n    hoisted<T>(factory: () => T): T {\n      if (typeof factory !== 'function') {\n        throw new TypeError(\n          `vi.hoisted() expects a function, but received a ${typeof factory}`,\n        )\n      }\n      return factory()\n    },\n\n    mock(path: string | Promise<unknown>, factory?: ModuleMockOptions | ModuleMockFactoryWithHelper): void {\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          ? () =>","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/mocker/src/browser/hints.ts#L37-L73","documentation":"Thrown at packages/mocker/src/browser/hints.ts:54-58 as a TypeError when vi.hoisted() is called with a non-function argument. vi.hoisted must receive a factory function because it executes it immediately at hoist time and returns the factory's return value, which is impossible without a callable.","triggerScenarios":"Calling vi.hoisted(undefined), vi.hoisted(null), vi.hoisted('value'), vi.hoisted({}), or vi.hoisted(someVariable) where the variable is not a function.","commonSituations":"Passing a value instead of a factory: vi.hoisted({ mock: true }); passing a variable that was conditionally assigned and is undefined in some path; copy-paste from vi.mock which takes a string.","solutions":["Pass a function to vi.hoisted: vi.hoisted(() => ({ mock: true })).","If you intended to share a value, wrap it in a factory that returns it."],"exampleFix":"// before\nconst data = vi.hoisted({ count: 0 })\n\n// after\nconst data = vi.hoisted(() => ({ count: 0 }))","handlingStrategy":"type-guard","validationCode":"// Validate the factory is a function before calling vi.hoisted.\nfunction hoisted<T>(factory: () => T): T {\n  if (typeof factory !== 'function') {\n    throw new TypeError(`vi.hoisted() requires a function, got ${typeof factory}`)\n  }\n  return vi.hoisted(factory)\n}","typeGuard":"function isHoistFactory<T>(value: unknown): value is () => T {\n  return typeof value === 'function'\n}","tryCatchPattern":null,"preventionTips":["Always pass an arrow function: vi.hoisted(() => value).","Enable strict TypeScript so non-function arguments are caught at compile time.","Avoid reusing vi.mock's string-argument muscle memory with vi.hoisted."],"tags":["vi-hoisted","type-validation","typeerror"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}