Skip to content

Commit 35d8b79

Browse files
authored
fix: compat jest plugin hooks (#6641)
1 parent 5c97ec2 commit 35d8b79

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed

.changeset/cool-cooks-sell.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@modern-js/app-tools': patch
3+
---
4+
5+
fix: compat jest plugin hooks
6+
7+
fix: 兼容 jest 插件 hooks 函数

packages/solutions/app-tools/src/compat/hooks.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ export function getHookRunners(
8484
const result = await (hooks as any)?.appendEntryCode.call(params);
8585
return result;
8686
},
87+
// test plugin hooks
88+
jestConfig: async (utils: any) => {
89+
const result = await (hooks as any)?.jestConfig.call(
90+
utils,
91+
(utils: any) => utils,
92+
);
93+
return result;
94+
},
95+
afterTest: async () => {
96+
return (hooks as any).afterTest.call();
97+
},
8798

8899
/**
89100
* common hooks
@@ -192,9 +203,17 @@ export function handleSetupResult(
192203
if (typeof fn === 'function') {
193204
const newAPI = transformHookRunner(key);
194205
if (api[newAPI]) {
195-
api[newAPI](async (params: any) =>
196-
transformHookResult(key, await fn(transformHookParams(key, params))),
197-
);
206+
api[newAPI](async (...params: any) => {
207+
const { isMultiple, params: transformParams } = transformHookParams(
208+
key,
209+
params,
210+
);
211+
if (isMultiple) {
212+
return transformHookResult(key, await fn(...transformParams));
213+
} else {
214+
return transformHookResult(key, await fn(transformParams));
215+
}
216+
});
198217
}
199218
}
200219
});

packages/solutions/app-tools/src/compat/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createCollectAsyncHook } from '@modern-js/plugin-v2';
1+
import { createAsyncHook, createCollectAsyncHook } from '@modern-js/plugin-v2';
22
import type { Entrypoint } from '@modern-js/types';
33
import type { AppTools, CliPluginFuture } from '../types';
44
import { getHookRunners } from './hooks';
@@ -7,6 +7,11 @@ type AppendEntryCodeFn = (params: {
77
entrypoint: Entrypoint;
88
code: string;
99
}) => string | Promise<string>;
10+
type JestConfigFn = (
11+
utils: any,
12+
next: (utils: any) => any,
13+
) => void | Promise<void>;
14+
type AfterTestFn = () => void | Promise<void>;
1015

1116
export const compatPlugin = (): CliPluginFuture<AppTools<'shared'>> => ({
1217
name: '@modern-js/app-tools-compat',
@@ -35,6 +40,8 @@ export const compatPlugin = (): CliPluginFuture<AppTools<'shared'>> => ({
3540
},
3641
registryHooks: {
3742
appendEntryCode: createCollectAsyncHook<AppendEntryCodeFn>(),
43+
jestConfig: createAsyncHook<JestConfigFn>(),
44+
afterTest: createAsyncHook<AfterTestFn>(),
3845
},
3946
setup: api => {
4047
api.updateAppContext({

packages/solutions/app-tools/src/compat/utils.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,42 @@ export function transformHookRunner(hookRunnerName: string) {
4848
}
4949
}
5050

51+
/**
52+
* Note:
53+
* isMultiple Indicates whether the function parameter represents multiple values.
54+
*/
5155
export function transformHookParams(hookRunnerName: string, params: any) {
5256
switch (hookRunnerName) {
5357
case 'resolvedConfig':
5458
return {
55-
resolved: params,
59+
isMultiple: false,
60+
params: {
61+
resolved: params[0],
62+
},
5663
};
5764
case 'htmlPartials':
5865
return {
59-
partials: {
60-
top: params.partials.top.current,
61-
head: params.partials.head.current,
62-
body: params.partials.body.current,
66+
isMultiple: false,
67+
params: {
68+
partials: {
69+
top: params.partials.top.current,
70+
head: params.partials.head.current,
71+
body: params.partials.body.current,
72+
},
73+
entrypoint: params.entrypoint,
6374
},
64-
entrypoint: params.entrypoint,
6575
};
76+
case 'jestConfig': {
77+
return {
78+
isMultiple: true,
79+
params: params,
80+
};
81+
}
6682
default:
67-
return params;
83+
return {
84+
isMultiple: false,
85+
params: params[0],
86+
};
6887
}
6988
}
7089

0 commit comments

Comments
 (0)