{"record":{"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":"critical","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          {cause: error},\n        );\n      }\n    }\n  }\n}\n","sourceCodeStart":57,"sourceCodeEnd":86,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/jest-core/src/runGlobalHook.ts#L57-L86","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)."],"exampleFix":"// before: jest.config.js\nmodule.exports = { globalSetup: './setup.ts' };\n// setup.ts — throws because DB is down\nexport default async () => { await db.connect(); };\n\n// after: add graceful error with context\nexport default async () => {\n  try { await db.connect(); }\n  catch (e) { throw new Error(`globalSetup DB connection failed: ${e.message}`); }\n};","handlingStrategy":"try-catch","validationCode":"// Validate the global setup file exports a function before running Jest\nconst fs = require('fs');\nfunction validateGlobalHook(modulePath) {\n  if (!fs.existsSync(modulePath)) {\n    throw new Error(`globalSetup file not found: ${modulePath}`);\n  }\n  // dry-load to check it exports a function\n  const mod = require(modulePath);\n  if (typeof mod !== 'function' && typeof mod.default !== 'function') {\n    throw new Error('globalSetup must export a function');\n  }\n}","typeGuard":"function isGlobalHookExport(mod: unknown): mod is (g: unknown, p: unknown) => Promise<void> {\n  return typeof mod === 'function' || (typeof mod === 'object' && mod !== null && typeof (mod as any).default === 'function');\n}","tryCatchPattern":"// In the globalSetup file itself — wrap risky operations\nexport default async function globalSetup() {\n  try {\n    await initializeDatabase();\n  } catch (error) {\n    throw new Error(`globalSetup failed during DB init: ${(error as Error).message}`, { cause: error });\n  }\n}","preventionTips":["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."],"tags":["jest-core","global-setup","configuration","lifecycle-hook"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}