{"record":{"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":"exception","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/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/browser/hints.ts#L37-L73","documentation":"`vi.hoisted` hoists a factory to the top of the module so its return value is available before imports. The factory must be a function; otherwise hoisting has nothing to invoke. The browser hints layer throws a `TypeError` reporting the actual `typeof` value received.","triggerScenarios":"Calling `vi.hoisted(42)`, `vi.hoisted('value')`, `vi.hoisted(undefined)`, or passing a config object by mistake instead of a factory function.","commonSituations":"Copy-paste error where a value is passed instead of `() => value`; refactor that changed the factory into a direct call (`vi.hoisted(makeMock())` instead of `vi.hoisted(() => makeMock())`).","solutions":["Wrap the value in a zero-arg function: `vi.hoisted(() => value)`.","If you intended to pass config, note `vi.hoisted` takes only a factory — return the config from it.","Double-check the call site after refactors that inline the factory body."],"exampleFix":"// before\nconst mock = vi.hoisted({ foo: 'bar' })\n\n// after\nconst mock = vi.hoisted(() => ({ foo: 'bar' }))","handlingStrategy":"type-guard","validationCode":"if (typeof factory !== 'function') {\n  throw new TypeError(`vi.hoisted requires a function, got ${typeof factory}`)\n}","typeGuard":"const isFactory = (v: unknown): v is () => unknown =>\n  typeof v === 'function'","tryCatchPattern":null,"preventionTips":["Always pass a zero-arg arrow function to vi.hoisted.","Do not invoke the factory inline — return its result from the callback.","Re-check hoisted call sites after refactors."],"tags":["mocker","vi-hoisted","type-error","browser"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}