From 35d8b797bfd71e0cc0a4724c307b7e4b05c69097 Mon Sep 17 00:00:00 2001 From: Belinda Cao Date: Wed, 18 Dec 2024 11:38:12 +0800 Subject: [PATCH] fix: compat jest plugin hooks (#6641) --- .changeset/cool-cooks-sell.md | 7 ++++ .../solutions/app-tools/src/compat/hooks.ts | 25 ++++++++++++-- .../solutions/app-tools/src/compat/index.ts | 9 ++++- .../solutions/app-tools/src/compat/utils.ts | 33 +++++++++++++++---- 4 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 .changeset/cool-cooks-sell.md diff --git a/.changeset/cool-cooks-sell.md b/.changeset/cool-cooks-sell.md new file mode 100644 index 00000000000..17078b0e279 --- /dev/null +++ b/.changeset/cool-cooks-sell.md @@ -0,0 +1,7 @@ +--- +'@modern-js/app-tools': patch +--- + +fix: compat jest plugin hooks + +fix: 兼容 jest 插件 hooks 函数 diff --git a/packages/solutions/app-tools/src/compat/hooks.ts b/packages/solutions/app-tools/src/compat/hooks.ts index 283ed49e023..f2e9b74b024 100644 --- a/packages/solutions/app-tools/src/compat/hooks.ts +++ b/packages/solutions/app-tools/src/compat/hooks.ts @@ -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 @@ -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)); + } + }); } } }); diff --git a/packages/solutions/app-tools/src/compat/index.ts b/packages/solutions/app-tools/src/compat/index.ts index 3674d229e84..857d58553e0 100644 --- a/packages/solutions/app-tools/src/compat/index.ts +++ b/packages/solutions/app-tools/src/compat/index.ts @@ -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'; @@ -7,6 +7,11 @@ type AppendEntryCodeFn = (params: { entrypoint: Entrypoint; code: string; }) => string | Promise; +type JestConfigFn = ( + utils: any, + next: (utils: any) => any, +) => void | Promise; +type AfterTestFn = () => void | Promise; export const compatPlugin = (): CliPluginFuture> => ({ name: '@modern-js/app-tools-compat', @@ -35,6 +40,8 @@ export const compatPlugin = (): CliPluginFuture> => ({ }, registryHooks: { appendEntryCode: createCollectAsyncHook(), + jestConfig: createAsyncHook(), + afterTest: createAsyncHook(), }, setup: api => { api.updateAppContext({ diff --git a/packages/solutions/app-tools/src/compat/utils.ts b/packages/solutions/app-tools/src/compat/utils.ts index 724db281885..a803621e535 100644 --- a/packages/solutions/app-tools/src/compat/utils.ts +++ b/packages/solutions/app-tools/src/compat/utils.ts @@ -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], + }; } }