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
- Read the text after `reason:` in the message — that is the real underlying error, not the wrapper.
- Run the hook file standalone with `node --loader ts-node/esm ./globalSetup.ts` (or compiled JS) to reproduce outside Jest.
- Confirm the file exports exactly one function: `module.exports = async function (globalConfig, projectConfig) { ... }`.
- Verify every env var / secret / network endpoint the hook needs is present in the CI shell, not just local .env.
- 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
- Export a single async function, not an object.
- Test the hook file standalone with node before committing.
- Read every env var via a typed config loader that fails with a clear message on missing keys.
- Avoid throwing frozen Error subclasses from inside the hook.
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
- Shard ${globalConfig.shard.shardIndex}/${globalConfig.shard.
- Watch plugin configuration error
- JSDOM did not return a Window object
- The --config option requires a JSON string literal, or a fil
- Cannot merge config in form of callback
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/2bdc9cebd02597ac.json.
Report an issue: GitHub.