jestjs/jest · error · TypeError

file must export a function at

Error message

${moduleName} file must export a function at ${modulePath}

What it means

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.

Solutions

  1. Ensure the file has a default/sole export that is an async or sync function
  2. For CommonJS: module.exports = async (globalConfig, projectConfig) => { ... }
  3. For ESM: export default async (globalConfig, projectConfig) => { ... }
  4. Verify the export with console.log(typeof require('./globalSetup'))

Example fix

// before: global-setup.js
module.exports = { setup: async () => { /* ... */ } };

// after
module.exports = async (globalConfig, projectConfig) => {
  /* ... */
};
Defensive patterns

Strategy: type-guard

Validate before calling

const mod = require('./global-setup');
if (typeof mod !== 'function') {
  throw new TypeError('globalSetup must export a function');
}

Type guard

function isGlobalHookExport(mod: unknown): mod is (globalConfig: unknown, projectConfig: unknown) => unknown | Promise<unknown> {
  return typeof mod === 'function';
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10). Data as JSON: /api/errors/bad3712ba1564e10. Report an issue: GitHub.

Appendix: source

Thrown at packages/jest-core/src/runGlobalHook.ts:53

      }

      const correctConfig = allTests.find(
        t => t.context.config[moduleName] === modulePath,
      );

      const projectConfig = correctConfig
        ? correctConfig.context.config
        : // Fallback to first config
          allTests[0].context.config;

      const transformer = await createScriptTransformer(projectConfig);

      try {
        await transformer.requireAndTranspileModule(
          modulePath,
          async globalModule => {
            if (typeof globalModule !== 'function') {
              throw new TypeError(
                `${moduleName} file must export a function at ${modulePath}`,
              );
            }

            await globalModule(globalConfig, projectConfig);
          },
        );
      } catch (error) {
        if (
          isError(error) &&
          (Object.getOwnPropertyDescriptor(error, 'message')?.writable ||
            Object.getOwnPropertyDescriptor(
              Object.getPrototypeOf(error),
              'message',
            )?.writable)
        ) {
          error.message = `Jest: Got error running ${moduleName} - ${modulePath}, reason: ${error.message}`;

View on GitHub (pinned to 8e6d128e4a)