{"id":"2bdc9cebd02597ac","repo":"jestjs/jest","slug":"jest-got-error-running-modulename-modulepa","errorCode":null,"errorMessage":"Jest: Got error running ${moduleName} - ${modulePath}, reason: ${prettyFormat(error, {maxDepth: 3})}","messagePattern":"Jest: Got error running (.+?) - (.+?), reason: (.+?)\\)\\}","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-core/src/runGlobalHook.ts","lineNumber":75,"sourceCode":"\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\n          throw error;\n        }\n\n        throw new Error(\n          `Jest: Got error running ${moduleName} - ${modulePath}, reason: ${prettyFormat(\n            error,\n            {maxDepth: 3},\n          )}`,\n        );\n      }\n    }\n  }\n}\n","sourceCodeStart":57,"sourceCodeEnd":85,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-core/src/runGlobalHook.ts#L57-L85","documentation":"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.","triggerScenarios":"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).","commonSituations":"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 () => {}`.","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."],"exampleFix":"// before\nmodule.exports = {\n  setup: async () => {\n    await db.connect();\n  },\n};\n\n// after\nmodule.exports = async function (globalConfig, projectConfig) {\n  await db.connect(process.env.DATABASE_URL);\n};","handlingStrategy":"try-catch","validationCode":"// before wiring the hook, confirm it exports an async function\nconst mod = require('./globalSetup');\nif (typeof mod !== 'function') {\n  throw new Error('globalSetup must export a function');\n}","typeGuard":"const isGlobalHook = (m: unknown): m is (...args: unknown[]) => unknown | Promise<unknown> => typeof m === 'function';","tryCatchPattern":"module.exports = async function (globalConfig, projectConfig) {\n  try {\n    await riskyStartup(globalConfig);\n  } catch (err) {\n    // rethrow a plain Error so Jest can enrich message\n    throw new Error(`globalSetup failed: ${err instanceof Error ? err.message : String(err)}`);\n  }\n};","preventionTips":["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."],"tags":["jest-core","global-setup","config","startup"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}