11import { wrappedNodeFetch } from '../integrations/octokit/require' ;
22import { Response } from 'node-fetch' ;
3+ import fetch from 'node-fetch' ;
34import { createExecutionContext , getExecutionContext } from '../src/context' ;
45import { HTTP } from '../src/keploy' ;
56
67describe ( 'wrappedNodeFetch' , ( ) => {
78 it ( 'should call fetch function with correct arguments in record mode' , async ( ) => {
8- const mockFetch = jest . fn ( ) . mockResolvedValueOnce ( new Response ( ) ) ;
99 const ctx = {
1010 mode : 'record' ,
1111 testId : 'testId' ,
1212 mocks : [ ] ,
1313 deps : [ ] ,
1414 } ;
1515 createExecutionContext ( ctx )
16- const wrappedFetch = ( wrappedNodeFetch ( mockFetch ) as any ) . bind ( { fetch : mockFetch } ) ;
17- const url = 'http ://example.com ' ;
16+ const wrappedFetch = ( wrappedNodeFetch ( fetch ) as any ) . bind ( { fetch } ) ;
17+ const url = 'https ://api.keploy.io/healthz ' ;
1818 const options = {
1919 method : 'GET' ,
2020 } ;
21-
2221 const response = await wrappedFetch ( url , options ) ;
2322 const updatedctx = getExecutionContext ( ) . context ;
2423 const mocks = updatedctx . mocks . length ;
2524 const deps = updatedctx . deps . length ;
2625 const responseBody = await response . text ( ) ;
2726 const recordedOutput = updatedctx . mocks [ 0 ] . Spec . Res . Body ;
28- expect ( mockFetch ) . toHaveBeenCalledWith ( url , options ) ;
2927 expect ( response ) . toBeInstanceOf ( Response ) ;
3028 expect ( mocks ) . toBeGreaterThan ( 0 ) ;
3129 expect ( deps ) . toBeGreaterThan ( 0 ) ;
3230 expect ( response ) . toHaveProperty ( 'body' ) ;
3331 expect ( responseBody ) . toEqual ( recordedOutput ) ;
3432 } ) ;
3533
36- it ( 'should return mocked response in test mode' , async ( ) => {
34+ it ( 'should return mocked response in test mode' , async ( ) => {
3735 const mockResponse = new Response ( 'mocked response' ) ;
38- const mockFetch = jest . fn ( ) . mockResolvedValue ( mockResponse ) ;
3936 const ctx = {
4037 mode : 'test' ,
4138 testId : 'testId' ,
@@ -47,12 +44,12 @@ describe('wrappedNodeFetch', () => {
4744 Spec : {
4845 Metadata : {
4946 name : 'node-fetch' ,
50- url : 'http ://example.com ' ,
47+ url : 'https ://api.keploy.io/healthz ' ,
5148 options : { method : 'GET' } ,
5249 type : 'HTTP_CLIENT' ,
5350 } ,
5451 Req : {
55- URL : 'http ://example.com ' ,
52+ URL : 'https ://api.keploy.io/healthz ' ,
5653 Body : '' ,
5754 Header : { } ,
5855 Method : 'GET' ,
@@ -70,23 +67,25 @@ describe('wrappedNodeFetch', () => {
7067 } ;
7168 createExecutionContext ( ctx )
7269
73- const wrappedFetch = ( wrappedNodeFetch ( mockFetch ) as any ) . bind ( { fetch : mockFetch } ) ;
74- const url = 'http ://example.com ' ;
70+ const wrappedFetch = ( wrappedNodeFetch ( fetch ) as any ) . bind ( { fetch } ) ;
71+ const url = 'https ://api.keploy.io/healthz ' ;
7572 const options = {
7673 method : 'GET' ,
7774 } ;
7875 const response = await wrappedFetch ( url , options ) ;
7976 const updatedctx = getExecutionContext ( ) . context ;
80- expect ( response ) . toEqual ( mockResponse ) ;
81- const mocks = updatedctx . mocks . length ( ) ;
77+ expect ( response . status ) . toEqual ( mockResponse . status ) ;
78+ expect ( response . statusText ) . toEqual ( mockResponse . statusText ) ;
79+
80+ const mocks = updatedctx . mocks . length ;
8281 expect ( mocks ) . toBe ( 0 ) ;
8382 } ) ;
8483
8584 it ( 'should return undefined if execution context is not present in record mode' , async ( ) => {
8685 const mockFetch = jest . fn ( ) . mockResolvedValue ( new Response ( ) ) ;
8786 const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ) ;
8887 const wrappedFetch = ( wrappedNodeFetch ( mockFetch ) as any ) . bind ( { fetch : mockFetch } ) ;
89- const url = 'http ://example.com ' ;
88+ const url = 'https ://api.keploy.io/healthz ' ;
9089 const options = {
9190 method : 'GET' ,
9291 } ;
@@ -106,7 +105,7 @@ describe('wrappedNodeFetch', () => {
106105 createExecutionContext ( ctx )
107106
108107 const wrappedFetch = ( wrappedNodeFetch ( mockFetch ) as any ) . bind ( { fetch : mockFetch } ) ;
109- const url = 'http ://example.com ' ;
108+ const url = 'https ://api.keploy.io/healthz ' ;
110109 const options = {
111110 method : 'GET' ,
112111 } ;
0 commit comments