{"id":"bad3712ba1564e10","repo":"jestjs/jest","slug":"modulename-file-must-export-a-function-at-mod","errorCode":null,"errorMessage":"${moduleName} file must export a function at ${modulePath}","messagePattern":"(.+?) file must export a function at (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/jest-core/src/runGlobalHook.ts","lineNumber":53,"sourceCode":"      }\n\n      const correctConfig = allTests.find(\n        t => t.context.config[moduleName] === modulePath,\n      );\n\n      const projectConfig = correctConfig\n        ? correctConfig.context.config\n        : // Fallback to first config\n          allTests[0].context.config;\n\n      const transformer = await createScriptTransformer(projectConfig);\n\n      try {\n        await transformer.requireAndTranspileModule(\n          modulePath,\n          async globalModule => {\n            if (typeof globalModule !== 'function') {\n              throw new TypeError(\n                `${moduleName} file must export a function at ${modulePath}`,\n              );\n            }\n\n            await globalModule(globalConfig, projectConfig);\n          },\n        );\n      } catch (error) {\n        if (\n          isError(error) &&\n          (Object.getOwnPropertyDescriptor(error, 'message')?.writable ||\n            Object.getOwnPropertyDescriptor(\n              Object.getPrototypeOf(error),\n              'message',\n            )?.writable)\n        ) {\n          error.message = `Jest: Got error running ${moduleName} - ${modulePath}, reason: ${error.message}`;\n","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-core/src/runGlobalHook.ts#L35-L71","documentation":"runGlobalHook loads each configured globalSetup/globalTeardown module via the script transformer and expects its default export (or module.exports) to be a function. If the module exports an object, a string, or nothing, this TypeError is thrown naming the module path. The function receives (globalConfig, projectConfig).","triggerScenarios":"A globalSetup file that does `export default {...}` instead of `export default function(...) {...}`; a module with only named exports; a file that forgot to export anything.","commonSituations":"Converting a config-object file into a globalSetup but forgetting to change the export; a refactor that drops the default export; CommonJS/ESM interop returning the module namespace instead of the function.","solutions":["Make the globalSetup/globalTeardown file export a function as default: export default async (globalConfig, projectConfig) => {...}","For CommonJS: module.exports = async (globalConfig, projectConfig) => {...}","Verify there is no accidental named-only export or object export"],"exampleFix":"// before\nexport default { setup: () => { /*...*/ } };\n// after\nexport default async (globalConfig, projectConfig) => { /*...*/ };","handlingStrategy":"type-guard","validationCode":"function assertFunctionExport(m: unknown): asserts m is Function {\n  if (typeof m !== 'function') {\n    throw new TypeError('globalSetup/globalTeardown must default-export a function');\n  }\n}","typeGuard":"function isGlobalHookExport(m: unknown): m is (g: unknown, p: unknown) => unknown | Promise<unknown> {\n  return typeof m === 'function';\n}","tryCatchPattern":null,"preventionTips":["Always default-export a function from globalSetup/globalTeardown files","Add a smoke-import test asserting typeof default === 'function'"],"tags":["config","global-hooks","typescript"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}