Skip to content

Commit

Permalink
fix: execute a global hook for all hooking projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Aetherall committed Apr 5, 2024
1 parent 6c49a41 commit eabce89
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions packages/jest-core/src/runGlobalHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,34 @@ export default async function runGlobalHook({
globalConfig: Config.GlobalConfig;
moduleName: 'globalSetup' | 'globalTeardown';
}): Promise<void> {
const globalModulePaths = new Set(
allTests.map(test => test.context.config[moduleName]),
const {configs} = allTests.reduce(
(acc, test) => {
const hook = test.context.config[moduleName];
if (!hook) return acc;

const id = test.context.config.id;
const registered = acc.ids.includes(id);

Check failure on line 29 in packages/jest-core/src/runGlobalHook.ts

View workflow job for this annotation

GitHub Actions / Typecheck Examples and Tests

Argument of type 'string' is not assignable to parameter of type 'never'.

Check failure on line 29 in packages/jest-core/src/runGlobalHook.ts

View workflow job for this annotation

GitHub Actions / TypeScript Compatibility

Argument of type 'string' is not assignable to parameter of type 'never'.
if (registered) return acc;

acc.ids.push(id);

Check failure on line 32 in packages/jest-core/src/runGlobalHook.ts

View workflow job for this annotation

GitHub Actions / Typecheck Examples and Tests

Argument of type 'string' is not assignable to parameter of type 'never'.

Check failure on line 32 in packages/jest-core/src/runGlobalHook.ts

View workflow job for this annotation

GitHub Actions / TypeScript Compatibility

Argument of type 'string' is not assignable to parameter of type 'never'.
acc.configs.push(test.context.config);

Check failure on line 33 in packages/jest-core/src/runGlobalHook.ts

View workflow job for this annotation

GitHub Actions / Typecheck Examples and Tests

Argument of type 'ProjectConfig' is not assignable to parameter of type 'never'.

Check failure on line 33 in packages/jest-core/src/runGlobalHook.ts

View workflow job for this annotation

GitHub Actions / TypeScript Compatibility

Argument of type 'ProjectConfig' is not assignable to parameter of type 'never'.
return acc;
},
{ids: [], configs: []},

Check failure on line 36 in packages/jest-core/src/runGlobalHook.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected object keys to be in ascending order. 'configs' should be before 'ids'
);

if (globalConfig[moduleName]) {
globalModulePaths.add(globalConfig[moduleName]);
configs.push(globalConfig);

Check failure on line 40 in packages/jest-core/src/runGlobalHook.ts

View workflow job for this annotation

GitHub Actions / Typecheck Examples and Tests

Argument of type 'GlobalConfig' is not assignable to parameter of type 'never'.

Check failure on line 40 in packages/jest-core/src/runGlobalHook.ts

View workflow job for this annotation

GitHub Actions / TypeScript Compatibility

Argument of type 'GlobalConfig' is not assignable to parameter of type 'never'.
}

if (globalModulePaths.size > 0) {
for (const modulePath of globalModulePaths) {
if (configs.length > 0) {
for (const config of configs) {
const modulePath = config[moduleName];
if (!modulePath) {
continue;
}

const correctConfig = allTests.find(
t => t.context.config[moduleName] === modulePath,
);

const projectConfig = correctConfig
? correctConfig.context.config
: // Fallback to first config
allTests[0].context.config;

const transformer = await createScriptTransformer(projectConfig);
const transformer = await createScriptTransformer(config);

try {
await transformer.requireAndTranspileModule(
Expand All @@ -55,7 +59,7 @@ export default async function runGlobalHook({
);
}

await globalModule(globalConfig, projectConfig);
await globalModule(globalConfig, config);
},
);
} catch (error) {
Expand Down

0 comments on commit eabce89

Please sign in to comment.