11import { wrappedNodeFetch } from '../integrations/octokit/require' ;
22import { Response } from 'node-fetch' ;
3- import { createExecutionContext } from '../src/context' ;
3+ import { createExecutionContext , getExecutionContext } from '../src/context' ;
44describe ( 'wrappedNodeFetch' , ( ) => {
55 it ( 'should call fetch function with correct arguments in record mode' , async ( ) => {
66 const mockFetch = jest . fn ( ) . mockResolvedValueOnce ( new Response ( ) ) ;
77 const ctx = {
8- context :{
98 mode : 'record' ,
109 testId : 'testId' ,
1110 mocks : [ ] ,
12- deps : [ ] ,
13- }
11+ deps : [ ] ,
1412 } ;
1513 createExecutionContext ( ctx )
1614 const wrappedFetch = ( wrappedNodeFetch ( mockFetch ) as any ) . bind ( { fetch : mockFetch } ) ;
1715 const url = 'http://example.com' ;
1816 const options = {
1917 method : 'GET' ,
2018 } ;
19+
2120 const response = await wrappedFetch ( url , options ) ;
21+ const updatedctx = getExecutionContext ( ) . context ;
22+ const mocks = updatedctx . mocks . length ;
23+ const deps = updatedctx . deps . length ;
2224 expect ( mockFetch ) . toHaveBeenCalledWith ( url , options ) ;
2325 expect ( response ) . toBeInstanceOf ( Response ) ;
26+ expect ( mocks ) . toBeGreaterThan ( 0 ) ;
27+ expect ( deps ) . toBeGreaterThan ( 0 ) ;
2428 } ) ;
2529
2630 it ( 'should return mocked response in test mode' , async ( ) => {
2731 const mockResponse = new Response ( 'mocked response' ) ;
2832 const mockFetch = jest . fn ( ) . mockResolvedValue ( mockResponse ) ;
2933 const ctx = {
30- context :{
3134 mode : 'test' ,
3235 testId : 'testId' ,
3336 mocks : [
@@ -38,12 +41,12 @@ describe('wrappedNodeFetch', () => {
3841 Spec : {
3942 Metadata : {
4043 name : 'node-fetch' ,
41- url : 'http://example.com ' ,
44+ url : 'http://localhost:8080 ' ,
4245 options : { method : 'GET' } ,
4346 type : 'HTTP_CLIENT' ,
4447 } ,
4548 Req : {
46- URL : 'http://example.com ' ,
49+ URL : 'http://localhost:8080 ' ,
4750 Body : '' ,
4851 Header : { } ,
4952 Method : 'GET' ,
@@ -57,7 +60,7 @@ describe('wrappedNodeFetch', () => {
5760 } ,
5861 ] ,
5962 deps : [ ] ,
60- }
63+
6164 } ;
6265 createExecutionContext ( ctx )
6366
@@ -67,7 +70,10 @@ describe('wrappedNodeFetch', () => {
6770 method : 'GET' ,
6871 } ;
6972 const response = await wrappedFetch ( url , options ) ;
73+ const updatedctx = getExecutionContext ( ) . context ;
7074 expect ( response ) . toEqual ( mockResponse ) ;
75+ const mocks = updatedctx . mocks . length ( ) ;
76+ expect ( mocks ) . toBe ( 0 ) ;
7177 } ) ;
7278
7379 it ( 'should return undefined if execution context is not present in record mode' , async ( ) => {
@@ -86,12 +92,10 @@ describe('wrappedNodeFetch', () => {
8692 it ( 'should call fetch function with correct arguments in off mode' , async ( ) => {
8793 const mockFetch = jest . fn ( ) . mockResolvedValueOnce ( new Response ( ) ) ;
8894 const ctx = {
89- context :{
9095 context : 'off' ,
9196 testId : 'testId' ,
9297 mocks : [ ] ,
9398 deps : [ ] ,
94- }
9599 } ;
96100 createExecutionContext ( ctx )
97101
0 commit comments