jestjs/jest · error · Error

Jest: Got error running ${moduleName} - ${modulePath}, reaso

Error message

Jest: Got error running ${moduleName} - ${modulePath}, reason: ${prettyFormat(error, {maxDepth: 3})}

What it means

Jest wraps any failure that occurs while loading or executing a globalSetup/globalTeardown module so the offending hook path and underlying reason are surfaced together. The error first tries to mutate `error.message` in place; if that property is non-writable (frozen/custom Error subclass), it falls back to constructing a fresh Error with the reason pretty-formatted at maxDepth 3 (line 75). Either way the run aborts because a failing global hook means the test environment cannot be reliably prepared or torn down.

Source

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

            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}`;

          throw error;
        }

        throw new Error(
          `Jest: Got error running ${moduleName} - ${modulePath}, reason: ${prettyFormat(
            error,
            {maxDepth: 3},
          )}`,
        );
      }
    }
  }
}

View on GitHub (pinned to f49721c78e)

Solutions

  1. Read the text after `reason:` in the message — that is the real underlying error, not the wrapper.
  2. Run the hook file standalone with `node --loader ts-node/esm ./globalSetup.ts` (or compiled JS) to reproduce outside Jest.
  3. Confirm the file exports exactly one function: `module.exports = async function (globalConfig, projectConfig) { ... }`.
  4. Verify every env var / secret / network endpoint the hook needs is present in the CI shell, not just local .env.
  5. If the underlying error is a frozen Error subclass, give it a writable `message` or rethrow a plain Error so Jest can enrich it.

Example fix

// before
module.exports = {
  setup: async () => {
    await db.connect();
  },
};

// after
module.exports = async function (globalConfig, projectConfig) {
  await db.connect(process.env.DATABASE_URL);
};
Defensive patterns

Strategy: try-catch

Validate before calling

// before wiring the hook, confirm it exports an async function
const mod = require('./globalSetup');
if (typeof mod !== 'function') {
  throw new Error('globalSetup must export a function');
}

Type guard

const isGlobalHook = (m: unknown): m is (...args: unknown[]) => unknown | Promise<unknown> => typeof m === 'function';

Try / catch

module.exports = async function (globalConfig, projectConfig) {
  try {
    await riskyStartup(globalConfig);
  } catch (err) {
    // rethrow a plain Error so Jest can enrich message
    throw new Error(`globalSetup failed: ${err instanceof Error ? err.message : String(err)}`);
  }
};

Prevention

When it happens

Trigger: Configuring `globalSetup` or `globalTeardown` to a module that throws on import (syntax error, missing dependency), exports something other than a function (line 53 throws TypeError for that), or whose exported function rejects/synchronously throws when invoked with (globalConfig, projectConfig).

Common situations: globalSetup connects to a database/container that is down or misconfigured; the hook file has a typo or references an unset env var; a transitive import breaks after a dependency upgrade; the file uses `module.exports = {setup}` (object) instead of `module.exports = async () => {}`.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/2bdc9cebd02597ac.json. Report an issue: GitHub.