MCPcopy
hub / github.com/jestjs/jest / runGlobalHook

Function runGlobalHook

packages/jest-core/src/runGlobalHook.ts:14–84  ·  view source on GitHub ↗
({
  allTests,
  globalConfig,
  moduleName,
}: {
  allTests: Array<Test>;
  globalConfig: Config.GlobalConfig;
  moduleName: 'globalSetup' | 'globalTeardown';
})

Source from the content-addressed store, hash-verified

12import prettyFormat from 'pretty-format';
13
14export default async function runGlobalHook({
15 allTests,
16 globalConfig,
17 moduleName,
18}: {
19 allTests: Array<Test>;
20 globalConfig: Config.GlobalConfig;
21 moduleName: 'globalSetup' | 'globalTeardown';
22}): Promise<void> {
23 const globalModulePaths = new Set(
24 allTests.map(test => test.context.config[moduleName]),
25 );
26
27 if (globalConfig[moduleName]) {
28 globalModulePaths.add(globalConfig[moduleName]);
29 }
30
31 if (globalModulePaths.size > 0) {
32 for (const modulePath of globalModulePaths) {
33 if (!modulePath) {
34 continue;
35 }
36
37 const correctConfig = allTests.find(
38 t => t.context.config[moduleName] === modulePath,
39 );
40
41 const projectConfig = correctConfig
42 ? correctConfig.context.config
43 : // Fallback to first config
44 allTests[0].context.config;
45
46 const transformer = await createScriptTransformer(projectConfig);
47
48 try {
49 await transformer.requireAndTranspileModule(
50 modulePath,
51 async globalModule => {
52 if (typeof globalModule !== 'function') {
53 throw new TypeError(
54 `${moduleName} file must export a function at ${modulePath}`,
55 );
56 }
57
58 await globalModule(globalConfig, projectConfig);
59 },
60 );
61 } catch (error) {
62 if (
63 isError(error) &&
64 (Object.getOwnPropertyDescriptor(error, 'message')?.writable ||
65 Object.getOwnPropertyDescriptor(
66 Object.getPrototypeOf(error),
67 'message',
68 )?.writable)
69 ) {
70 error.message = `Jest: Got error running ${moduleName} - ${modulePath}, reason: ${error.message}`;
71

Callers 2

_bailIfNeededMethod · 0.85
runJestFunction · 0.85

Calls 5

createScriptTransformerFunction · 0.90
isErrorFunction · 0.50
addMethod · 0.45

Tested by

no test coverage detected