{"record":{"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/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/jest-core/src/runGlobalHook.ts#L35-L71","documentation":"Thrown as a TypeError by runGlobalHook when the module referenced by globalSetup or globalTeardown (in project or global config) does not export a function as its default/sole export. Jest invokes these modules as functions with (globalConfig, projectConfig); a non-function export cannot be called.","triggerScenarios":"A globalSetup/globalTeardown file that exports an object, a constant, or uses named exports only (module.exports = { setup } instead of module.exports = setup); a file whose default export is accidentally undefined due to a circular import or missing return.","commonSituations":"Refactoring a setup file from a function export to an object with methods and forgetting Jest's contract; ESM configs using named exports where default is expected; TS files that export a const but no callable.","solutions":["Ensure the file has a default/sole export that is an async or sync function","For CommonJS: module.exports = async (globalConfig, projectConfig) => { ... }","For ESM: export default async (globalConfig, projectConfig) => { ... }","Verify the export with console.log(typeof require('./globalSetup'))"],"exampleFix":"// before: global-setup.js\nmodule.exports = { setup: async () => { /* ... */ } };\n\n// after\nmodule.exports = async (globalConfig, projectConfig) => {\n  /* ... */\n};","handlingStrategy":"type-guard","validationCode":"const mod = require('./global-setup');\nif (typeof mod !== 'function') {\n  throw new TypeError('globalSetup must export a function');\n}","typeGuard":"function isGlobalHookExport(mod: unknown): mod is (globalConfig: unknown, projectConfig: unknown) => unknown | Promise<unknown> {\n  return typeof mod === 'function';\n}","tryCatchPattern":null,"preventionTips":["Use module.exports = async (globalConfig, projectConfig) => { ... } for globalSetup/globalTeardown","For ESM, use export default async (globalConfig, projectConfig) => { ... }","Add a smoke test that requires the hook file and asserts typeof === 'function'"],"tags":["core","global-hooks","config","contract","argument-validation"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}