@@ -10,8 +10,15 @@ OF ANY KIND, either express or implied. See the License for the specific languag
10
10
governing permissions and limitations under the License.
11
11
*/
12
12
13
+ import { mockSetResultAndStopExecution } from '../__mocks__/setResultAndStopExecution' ;
13
14
import getAfterAllHookHandler , { AfterAllHookBuildConfig } from '../handleAfterAllHooks' ;
14
- import { HookResponse , HookStatus , GraphQLResult , HookFunctionPayloadContext } from '../types' ;
15
+ import {
16
+ HookResponse ,
17
+ HookStatus ,
18
+ GraphQLResult ,
19
+ HookFunctionPayloadContext ,
20
+ AfterAllHookResponse ,
21
+ } from '../types' ;
15
22
import { mockLogger } from '../__mocks__/yogaLogger' ;
16
23
import { describe , expect , test , vi , beforeEach } from 'vitest' ;
17
24
@@ -60,9 +67,13 @@ describe('getAfterAllHookHandler (afterAll)', () => {
60
67
afterAll : { blocking : true , module : { mockHook } , fn : 'mockHook' } ,
61
68
} ;
62
69
const handler = getAfterAllHookHandler ( mockConfig ) ;
63
- const result = await handler ( { payload : basePayload } ) ;
64
- expect ( result ) . toEqual ( mockResponse ) ;
70
+ await handler ( {
71
+ payload : basePayload ,
72
+ setResultAndStopExecution : mockSetResultAndStopExecution ,
73
+ } ) ;
65
74
expect ( mockHook ) . toHaveBeenCalledOnce ( ) ;
75
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledOnce ( ) ;
76
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledWith ( basePayload . result ) ;
66
77
} ) ;
67
78
68
79
test ( 'throws if blocking and status is ERROR' , async ( ) => {
@@ -78,7 +89,9 @@ describe('getAfterAllHookHandler (afterAll)', () => {
78
89
afterAll : { blocking : true , module : { mockHook } , fn : 'mockHook' } ,
79
90
} ;
80
91
const handler = getAfterAllHookHandler ( mockConfig ) ;
81
- await expect ( handler ( { payload : basePayload } ) ) . rejects . toThrow ( 'fail' ) ;
92
+ await expect (
93
+ handler ( { payload : basePayload , setResultAndStopExecution : mockSetResultAndStopExecution } ) ,
94
+ ) . rejects . toThrow ( 'fail' ) ;
82
95
} ) ;
83
96
84
97
test ( 'does not throw if non-blocking and status is ERROR' , async ( ) => {
@@ -94,15 +107,19 @@ describe('getAfterAllHookHandler (afterAll)', () => {
94
107
afterAll : { blocking : false , module : { mockHook } , fn : 'mockHook' } ,
95
108
} ;
96
109
const handler = getAfterAllHookHandler ( mockConfig ) ;
97
- const result = await handler ( { payload : basePayload } ) ;
98
- expect ( result ) . toEqual ( mockResponse ) ;
110
+ await handler ( {
111
+ payload : basePayload ,
112
+ setResultAndStopExecution : mockSetResultAndStopExecution ,
113
+ } ) ;
99
114
expect ( mockHook ) . toHaveBeenCalledOnce ( ) ;
115
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledOnce ( ) ;
116
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledWith ( basePayload . result ) ;
100
117
} ) ;
101
118
102
119
test ( 'returns modified result if present in response' , async ( ) => {
103
120
vi . mocked ( utils . isModuleFn ) . mockReturnValue ( true ) ;
104
- const modifiedResult = { data : { foo : 'bar' } , errors : [ ] } ;
105
- const mockResponse : HookResponse = {
121
+ const modifiedResult = { data : { foo : 'bar' } , errors : [ ] , extensions : { } } ;
122
+ const mockResponse : AfterAllHookResponse = {
106
123
status : HookStatus . SUCCESS ,
107
124
message : 'modified' ,
108
125
data : { result : modifiedResult } ,
@@ -117,31 +134,13 @@ describe('getAfterAllHookHandler (afterAll)', () => {
117
134
afterAll : { blocking : true , module : { mockHook } , fn : 'mockHook' } ,
118
135
} ;
119
136
const handler = getAfterAllHookHandler ( mockConfig ) ;
120
- const result = await handler ( { payload : basePayload } ) ;
121
- expect ( result ) . toEqual ( mockResponse ) ;
122
- expect ( mockHook ) . toHaveBeenCalledOnce ( ) ;
123
- } ) ;
124
-
125
- test ( 'handles headers in response data' , async ( ) => {
126
- vi . mocked ( utils . isModuleFn ) . mockReturnValue ( true ) ;
127
- const mockResponse : HookResponse = {
128
- status : HookStatus . SUCCESS ,
129
- message : 'headers' ,
130
- data : { headers : { 'x-test' : 'abc' } } ,
131
- } ;
132
- const mockHook = vi . fn ( ) . mockResolvedValue ( mockResponse ) ;
133
- vi . mocked ( utils . getWrappedLocalModuleHookFunction ) . mockResolvedValue ( mockHook ) ;
134
- vi . mocked ( utils . getWrappedLocalHookFunction ) . mockResolvedValue ( mockHook ) ;
135
- const mockConfig : AfterAllHookBuildConfig = {
136
- memoizedFns : { } ,
137
- baseDir : '' ,
138
- logger : mockLogger ,
139
- afterAll : { blocking : true , module : { mockHook } , fn : 'mockHook' } ,
140
- } ;
141
- const handler = getAfterAllHookHandler ( mockConfig ) ;
142
- const result = await handler ( { payload : basePayload } ) ;
143
- expect ( result ) . toEqual ( mockResponse ) ;
137
+ await handler ( {
138
+ payload : basePayload ,
139
+ setResultAndStopExecution : mockSetResultAndStopExecution ,
140
+ } ) ;
144
141
expect ( mockHook ) . toHaveBeenCalledOnce ( ) ;
142
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledOnce ( ) ;
143
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledWith ( modifiedResult ) ;
145
144
} ) ;
146
145
147
146
test ( 'throws if hook throws (blocking)' , async ( ) => {
@@ -158,7 +157,9 @@ describe('getAfterAllHookHandler (afterAll)', () => {
158
157
afterAll : { blocking : true , module : { mockHook } , fn : 'mockHook' } ,
159
158
} ;
160
159
const handler = getAfterAllHookHandler ( mockConfig ) ;
161
- await expect ( handler ( { payload : basePayload } ) ) . rejects . toThrow ( 'fail!' ) ;
160
+ await expect (
161
+ handler ( { payload : basePayload , setResultAndStopExecution : mockSetResultAndStopExecution } ) ,
162
+ ) . rejects . toThrow ( 'fail!' ) ;
162
163
} ) ;
163
164
164
165
test ( 'throws error if no hook is defined' , async ( ) => {
@@ -174,9 +175,9 @@ describe('getAfterAllHookHandler (afterAll)', () => {
174
175
afterAll : { blocking : true } ,
175
176
} ;
176
177
const handler = getAfterAllHookHandler ( mockConfig ) ;
177
- await expect ( handler ( { payload : basePayload } ) ) . rejects . toThrow (
178
- 'Unable to invoke local function undefined' ,
179
- ) ;
178
+ await expect (
179
+ handler ( { payload : basePayload , setResultAndStopExecution : mockSetResultAndStopExecution } ) ,
180
+ ) . rejects . toThrow ( 'Unable to invoke local function undefined' ) ;
180
181
} ) ;
181
182
182
183
test ( 'uses memoized function if present' , async ( ) => {
@@ -190,9 +191,13 @@ describe('getAfterAllHookHandler (afterAll)', () => {
190
191
afterAll : { blocking : true } ,
191
192
} ;
192
193
const handler = getAfterAllHookHandler ( mockConfig ) ;
193
- const result = await handler ( { payload : basePayload } ) ;
194
- expect ( result ) . toEqual ( mockResponse ) ;
194
+ await handler ( {
195
+ payload : basePayload ,
196
+ setResultAndStopExecution : mockSetResultAndStopExecution ,
197
+ } ) ;
195
198
expect ( memoized ) . toHaveBeenCalledOnce ( ) ;
199
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledOnce ( ) ;
200
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledWith ( basePayload . result ) ;
196
201
} ) ;
197
202
198
203
test ( 'handles invalid response (missing status) for blocking' , async ( ) => {
@@ -207,7 +212,9 @@ describe('getAfterAllHookHandler (afterAll)', () => {
207
212
afterAll : { blocking : true , module : { mockHook } , fn : 'mockHook' } ,
208
213
} ;
209
214
const handler = getAfterAllHookHandler ( mockConfig ) ;
210
- await expect ( handler ( { payload : basePayload } ) ) . rejects . toThrow ( ) ;
215
+ await expect (
216
+ handler ( { payload : basePayload , setResultAndStopExecution : mockSetResultAndStopExecution } ) ,
217
+ ) . rejects . toThrow ( ) ;
211
218
} ) ;
212
219
213
220
test ( 'handles remote hook (success)' , async ( ) => {
@@ -223,9 +230,13 @@ describe('getAfterAllHookHandler (afterAll)', () => {
223
230
afterAll : { blocking : true , composer : 'https://remote' } ,
224
231
} ;
225
232
const handler = getAfterAllHookHandler ( mockConfig ) ;
226
- const result = await handler ( { payload : basePayload } ) ;
227
- expect ( result ) . toEqual ( { status : HookStatus . SUCCESS , message : 'remote ok' } ) ;
233
+ await handler ( {
234
+ payload : basePayload ,
235
+ setResultAndStopExecution : mockSetResultAndStopExecution ,
236
+ } ) ;
228
237
expect ( mockRemoteHook ) . toHaveBeenCalledOnce ( ) ;
238
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledOnce ( ) ;
239
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledWith ( basePayload . result ) ;
229
240
} ) ;
230
241
231
242
test ( 'handles remote hook (error, blocking)' , async ( ) => {
@@ -241,7 +252,12 @@ describe('getAfterAllHookHandler (afterAll)', () => {
241
252
afterAll : { blocking : true , composer : 'https://remote' } ,
242
253
} ;
243
254
const handler = getAfterAllHookHandler ( mockConfig ) ;
244
- await expect ( handler ( { payload : basePayload } ) ) . rejects . toThrow ( 'remote fail' ) ;
255
+ await expect (
256
+ handler ( {
257
+ payload : basePayload ,
258
+ setResultAndStopExecution : mockSetResultAndStopExecution ,
259
+ } ) ,
260
+ ) . rejects . toThrow ( 'remote fail' ) ;
245
261
} ) ;
246
262
247
263
test ( 'handles remote hook (error, non-blocking)' , async ( ) => {
@@ -257,9 +273,13 @@ describe('getAfterAllHookHandler (afterAll)', () => {
257
273
afterAll : { blocking : false , composer : 'https://remote' } ,
258
274
} ;
259
275
const handler = getAfterAllHookHandler ( mockConfig ) ;
260
- const result = await handler ( { payload : basePayload } ) ;
261
- expect ( result ) . toEqual ( { status : HookStatus . ERROR , message : 'remote fail' } ) ;
276
+ await handler ( {
277
+ payload : basePayload ,
278
+ setResultAndStopExecution : mockSetResultAndStopExecution ,
279
+ } ) ;
262
280
expect ( mockRemoteHook ) . toHaveBeenCalledOnce ( ) ;
281
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledOnce ( ) ;
282
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledWith ( basePayload . result ) ;
263
283
} ) ;
264
284
265
285
test ( 'handles case-insensitive status comparison' , async ( ) => {
@@ -275,9 +295,13 @@ describe('getAfterAllHookHandler (afterAll)', () => {
275
295
afterAll : { blocking : true , module : { mockHook } , fn : 'mockHook' } ,
276
296
} ;
277
297
const handler = getAfterAllHookHandler ( mockConfig ) ;
278
- const result = await handler ( { payload : basePayload } ) ;
279
- expect ( result ) . toEqual ( mockResponse ) ;
298
+ await handler ( {
299
+ payload : basePayload ,
300
+ setResultAndStopExecution : mockSetResultAndStopExecution ,
301
+ } ) ;
280
302
expect ( mockHook ) . toHaveBeenCalledOnce ( ) ;
303
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledOnce ( ) ;
304
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledWith ( basePayload . result ) ;
281
305
} ) ;
282
306
283
307
test ( 'handles error with non-Error object' , async ( ) => {
@@ -294,7 +318,9 @@ describe('getAfterAllHookHandler (afterAll)', () => {
294
318
afterAll : { blocking : true , module : { mockHook } , fn : 'mockHook' } ,
295
319
} ;
296
320
const handler = getAfterAllHookHandler ( mockConfig ) ;
297
- await expect ( handler ( { payload : basePayload } ) ) . rejects . toThrow ( 'custom error object' ) ;
321
+ await expect (
322
+ handler ( { payload : basePayload , setResultAndStopExecution : mockSetResultAndStopExecution } ) ,
323
+ ) . rejects . toThrow ( 'custom error object' ) ;
298
324
} ) ;
299
325
300
326
test ( 'handles error without message property' , async ( ) => {
@@ -311,9 +337,9 @@ describe('getAfterAllHookHandler (afterAll)', () => {
311
337
afterAll : { blocking : true , module : { mockHook } , fn : 'mockHook' } ,
312
338
} ;
313
339
const handler = getAfterAllHookHandler ( mockConfig ) ;
314
- await expect ( handler ( { payload : basePayload } ) ) . rejects . toThrow (
315
- 'Error while invoking afterAll hook' ,
316
- ) ;
340
+ await expect (
341
+ handler ( { payload : basePayload , setResultAndStopExecution : mockSetResultAndStopExecution } ) ,
342
+ ) . rejects . toThrow ( 'Error while invoking afterAll hook' ) ;
317
343
} ) ;
318
344
319
345
test ( 'handles local function with composer path' , async ( ) => {
@@ -329,9 +355,13 @@ describe('getAfterAllHookHandler (afterAll)', () => {
329
355
afterAll : { blocking : true , composer : './local-hook.js' } ,
330
356
} ;
331
357
const handler = getAfterAllHookHandler ( mockConfig ) ;
332
- const result = await handler ( { payload : basePayload } ) ;
333
- expect ( result ) . toEqual ( mockResponse ) ;
358
+ await handler ( {
359
+ payload : basePayload ,
360
+ setResultAndStopExecution : mockSetResultAndStopExecution ,
361
+ } ) ;
334
362
expect ( mockHook ) . toHaveBeenCalledOnce ( ) ;
363
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledOnce ( ) ;
364
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledWith ( basePayload . result ) ;
335
365
} ) ;
336
366
337
367
test ( 'handles payload with null result' , async ( ) => {
@@ -352,9 +382,13 @@ describe('getAfterAllHookHandler (afterAll)', () => {
352
382
document : { } ,
353
383
result : null as unknown as GraphQLResult ,
354
384
} ;
355
- const result = await handler ( { payload : payloadWithNullResult } ) ;
356
- expect ( result ) . toEqual ( mockResponse ) ;
385
+ await handler ( {
386
+ payload : payloadWithNullResult ,
387
+ setResultAndStopExecution : mockSetResultAndStopExecution ,
388
+ } ) ;
357
389
expect ( mockHook ) . toHaveBeenCalledWith ( payloadWithNullResult ) ;
390
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledOnce ( ) ;
391
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledWith ( { } ) ;
358
392
} ) ;
359
393
360
394
test ( 'handles payload with undefined result' , async ( ) => {
@@ -375,8 +409,12 @@ describe('getAfterAllHookHandler (afterAll)', () => {
375
409
document : { } ,
376
410
result : undefined ,
377
411
} ;
378
- const result = await handler ( { payload : payloadWithUndefinedResult } ) ;
379
- expect ( result ) . toEqual ( mockResponse ) ;
380
- expect ( mockHook ) . toHaveBeenCalledWith ( payloadWithUndefinedResult ) ;
412
+ await handler ( {
413
+ payload : payloadWithUndefinedResult ,
414
+ setResultAndStopExecution : mockSetResultAndStopExecution ,
415
+ } ) ;
416
+ expect ( mockHook ) . toHaveBeenCalledOnce ( ) ;
417
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledOnce ( ) ;
418
+ expect ( mockSetResultAndStopExecution ) . toHaveBeenCalledWith ( { } ) ;
381
419
} ) ;
382
420
} ) ;
0 commit comments