File tree Expand file tree Collapse file tree 4 files changed +63
-11
lines changed
packages/solutions/app-tools/src/compat Expand file tree Collapse file tree 4 files changed +63
-11
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @modern-js/app-tools ' : patch
3
+ ---
4
+
5
+ fix: compat jest plugin hooks
6
+
7
+ fix: 兼容 jest 插件 hooks 函数
Original file line number Diff line number Diff line change @@ -84,6 +84,17 @@ export function getHookRunners(
84
84
const result = await ( hooks as any ) ?. appendEntryCode . call ( params ) ;
85
85
return result ;
86
86
} ,
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
+ } ,
87
98
88
99
/**
89
100
* common hooks
@@ -192,9 +203,17 @@ export function handleSetupResult(
192
203
if ( typeof fn === 'function' ) {
193
204
const newAPI = transformHookRunner ( key ) ;
194
205
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
+ } ) ;
198
217
}
199
218
}
200
219
} ) ;
Original file line number Diff line number Diff line change 1
- import { createCollectAsyncHook } from '@modern-js/plugin-v2' ;
1
+ import { createAsyncHook , createCollectAsyncHook } from '@modern-js/plugin-v2' ;
2
2
import type { Entrypoint } from '@modern-js/types' ;
3
3
import type { AppTools , CliPluginFuture } from '../types' ;
4
4
import { getHookRunners } from './hooks' ;
@@ -7,6 +7,11 @@ type AppendEntryCodeFn = (params: {
7
7
entrypoint : Entrypoint ;
8
8
code : string ;
9
9
} ) => string | Promise < string > ;
10
+ type JestConfigFn = (
11
+ utils : any ,
12
+ next : ( utils : any ) => any ,
13
+ ) => void | Promise < void > ;
14
+ type AfterTestFn = ( ) => void | Promise < void > ;
10
15
11
16
export const compatPlugin = ( ) : CliPluginFuture < AppTools < 'shared' > > => ( {
12
17
name : '@modern-js/app-tools-compat' ,
@@ -35,6 +40,8 @@ export const compatPlugin = (): CliPluginFuture<AppTools<'shared'>> => ({
35
40
} ,
36
41
registryHooks : {
37
42
appendEntryCode : createCollectAsyncHook < AppendEntryCodeFn > ( ) ,
43
+ jestConfig : createAsyncHook < JestConfigFn > ( ) ,
44
+ afterTest : createAsyncHook < AfterTestFn > ( ) ,
38
45
} ,
39
46
setup : api => {
40
47
api . updateAppContext ( {
Original file line number Diff line number Diff line change @@ -48,23 +48,42 @@ export function transformHookRunner(hookRunnerName: string) {
48
48
}
49
49
}
50
50
51
+ /**
52
+ * Note:
53
+ * isMultiple Indicates whether the function parameter represents multiple values.
54
+ */
51
55
export function transformHookParams ( hookRunnerName : string , params : any ) {
52
56
switch ( hookRunnerName ) {
53
57
case 'resolvedConfig' :
54
58
return {
55
- resolved : params ,
59
+ isMultiple : false ,
60
+ params : {
61
+ resolved : params [ 0 ] ,
62
+ } ,
56
63
} ;
57
64
case 'htmlPartials' :
58
65
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 ,
63
74
} ,
64
- entrypoint : params . entrypoint ,
65
75
} ;
76
+ case 'jestConfig' : {
77
+ return {
78
+ isMultiple : true ,
79
+ params : params ,
80
+ } ;
81
+ }
66
82
default :
67
- return params ;
83
+ return {
84
+ isMultiple : false ,
85
+ params : params [ 0 ] ,
86
+ } ;
68
87
}
69
88
}
70
89
You can’t perform that action at this time.
0 commit comments