Skip to content

Commit

Permalink
fix: compat jest plugin hooks (#6641)
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin authored Dec 18, 2024
1 parent 5c97ec2 commit 35d8b79
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .changeset/cool-cooks-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/app-tools': patch
---

fix: compat jest plugin hooks

fix: 兼容 jest 插件 hooks 函数
25 changes: 22 additions & 3 deletions packages/solutions/app-tools/src/compat/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ export function getHookRunners(
const result = await (hooks as any)?.appendEntryCode.call(params);
return result;
},
// test plugin hooks
jestConfig: async (utils: any) => {
const result = await (hooks as any)?.jestConfig.call(
utils,
(utils: any) => utils,
);
return result;
},
afterTest: async () => {
return (hooks as any).afterTest.call();
},

/**
* common hooks
Expand Down Expand Up @@ -192,9 +203,17 @@ export function handleSetupResult(
if (typeof fn === 'function') {
const newAPI = transformHookRunner(key);
if (api[newAPI]) {
api[newAPI](async (params: any) =>
transformHookResult(key, await fn(transformHookParams(key, params))),
);
api[newAPI](async (...params: any) => {
const { isMultiple, params: transformParams } = transformHookParams(
key,
params,
);
if (isMultiple) {
return transformHookResult(key, await fn(...transformParams));
} else {
return transformHookResult(key, await fn(transformParams));
}
});
}
}
});
Expand Down
9 changes: 8 additions & 1 deletion packages/solutions/app-tools/src/compat/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createCollectAsyncHook } from '@modern-js/plugin-v2';
import { createAsyncHook, createCollectAsyncHook } from '@modern-js/plugin-v2';
import type { Entrypoint } from '@modern-js/types';
import type { AppTools, CliPluginFuture } from '../types';
import { getHookRunners } from './hooks';
Expand All @@ -7,6 +7,11 @@ type AppendEntryCodeFn = (params: {
entrypoint: Entrypoint;
code: string;
}) => string | Promise<string>;
type JestConfigFn = (
utils: any,
next: (utils: any) => any,
) => void | Promise<void>;
type AfterTestFn = () => void | Promise<void>;

export const compatPlugin = (): CliPluginFuture<AppTools<'shared'>> => ({
name: '@modern-js/app-tools-compat',
Expand Down Expand Up @@ -35,6 +40,8 @@ export const compatPlugin = (): CliPluginFuture<AppTools<'shared'>> => ({
},
registryHooks: {
appendEntryCode: createCollectAsyncHook<AppendEntryCodeFn>(),
jestConfig: createAsyncHook<JestConfigFn>(),
afterTest: createAsyncHook<AfterTestFn>(),
},
setup: api => {
api.updateAppContext({
Expand Down
33 changes: 26 additions & 7 deletions packages/solutions/app-tools/src/compat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,42 @@ export function transformHookRunner(hookRunnerName: string) {
}
}

/**
* Note:
* isMultiple Indicates whether the function parameter represents multiple values.
*/
export function transformHookParams(hookRunnerName: string, params: any) {
switch (hookRunnerName) {
case 'resolvedConfig':
return {
resolved: params,
isMultiple: false,
params: {
resolved: params[0],
},
};
case 'htmlPartials':
return {
partials: {
top: params.partials.top.current,
head: params.partials.head.current,
body: params.partials.body.current,
isMultiple: false,
params: {
partials: {
top: params.partials.top.current,
head: params.partials.head.current,
body: params.partials.body.current,
},
entrypoint: params.entrypoint,
},
entrypoint: params.entrypoint,
};
case 'jestConfig': {
return {
isMultiple: true,
params: params,
};
}
default:
return params;
return {
isMultiple: false,
params: params[0],
};
}
}

Expand Down

0 comments on commit 35d8b79

Please sign in to comment.