jestjs/jest · critical · Error
Jest: Got error running
Error message
Jest: Got error running ${moduleName} - ${modulePath}, reason: ${prettyFormat(error, {maxDepth: 3})} What it means
Thrown when a globalSetup or globalTeardown hook module throws during execution. Jest catches the original error and re-wraps it with the module name and path for diagnosis. The pretty-format fallback (line 75) fires specifically when the original error's `message` property is not writable, so Jest cannot augment it in-place.
Solutions
- Read the `reason:` portion of the message — it contains the original error text or a pretty-format dump of the thrown value.
- Run the globalSetup/globalTeardown file directly with `node --import tsx path/to/setup.ts` to reproduce outside Jest.
- Verify the hook file default-exports an `async (globalConfig, projectConfig) => {}` function, not an object.
- Check that all environment variables and external services the hook depends on are available.
- If the pretty-format dump appears instead of a readable message, inspect the thrown value type — it may be a non-Error throw (e.g. a string or plain object).
Example fix
// before: jest.config.js
module.exports = { globalSetup: './setup.ts' };
// setup.ts — throws because DB is down
export default async () => { await db.connect(); };
// after: add graceful error with context
export default async () => {
try { await db.connect(); }
catch (e) { throw new Error(`globalSetup DB connection failed: ${e.message}`); }
}; Defensive patterns
Strategy: try-catch
Validate before calling
// Validate the global setup file exports a function before running Jest
const fs = require('fs');
function validateGlobalHook(modulePath) {
if (!fs.existsSync(modulePath)) {
throw new Error(`globalSetup file not found: ${modulePath}`);
}
// dry-load to check it exports a function
const mod = require(modulePath);
if (typeof mod !== 'function' && typeof mod.default !== 'function') {
throw new Error('globalSetup must export a function');
}
} Type guard
function isGlobalHookExport(mod: unknown): mod is (g: unknown, p: unknown) => Promise<void> {
return typeof mod === 'function' || (typeof mod === 'object' && mod !== null && typeof (mod as any).default === 'function');
} Try / catch
// In the globalSetup file itself — wrap risky operations
export default async function globalSetup() {
try {
await initializeDatabase();
} catch (error) {
throw new Error(`globalSetup failed during DB init: ${(error as Error).message}`, { cause: error });
}
} Prevention
- Run the globalSetup file standalone with node before integrating with Jest.
- Ensure the hook file default-exports a single async function.
- Wrap external dependencies (DB, APIs) in try-catch with descriptive messages.
- Use a pre-test npm script to validate config and hook files exist and export correctly.
When it happens
Trigger: Configuring `globalSetup` or `globalTeardown` in jest.config.js pointing to a module that either exports a non-function, throws at runtime, fails to connect to a database, or has a top-level syntax/import error. The pretty-format path at line 75 is hit when the thrown value is a frozen or non-standard error whose `message` descriptor is non-writable.
Common situations: Database connection failures in globalSetup, missing environment variables referenced by the hook, a transpile/transform error in the hook file, or the hook module default-exports an object instead of a function. Also triggered by errors thrown with `Object.freeze` on the error object or custom error classes with read-only message.
Related errors
- Shard / requested, but test sequencer in has no shard…
- Watch plugin configuration error
- Attempted to display seed but seed value is undefined
- babel-jest: Babel ignores
- Cannot parse as JSON
AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10).
Data as JSON: /api/errors/2bdc9cebd02597ac.
Report an issue: GitHub.
Appendix: 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},
)}`,
{cause: error},
);
}
}
}
}
View on GitHub (pinned to 8e6d128e4a)