1
1
import { wrappedNodeFetch } from '../integrations/octokit/require' ;
2
2
import { Response } from 'node-fetch' ;
3
+ import fetch from 'node-fetch' ;
3
4
import { createExecutionContext , getExecutionContext } from '../src/context' ;
4
5
import { HTTP } from '../src/keploy' ;
5
6
6
7
describe ( 'wrappedNodeFetch' , ( ) => {
7
8
it ( 'should call fetch function with correct arguments in record mode' , async ( ) => {
8
- const mockFetch = jest . fn ( ) . mockResolvedValueOnce ( new Response ( ) ) ;
9
9
const ctx = {
10
10
mode : 'record' ,
11
11
testId : 'testId' ,
12
12
mocks : [ ] ,
13
13
deps : [ ] ,
14
14
} ;
15
15
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 ' ;
18
18
const options = {
19
19
method : 'GET' ,
20
20
} ;
21
-
22
21
const response = await wrappedFetch ( url , options ) ;
23
22
const updatedctx = getExecutionContext ( ) . context ;
24
23
const mocks = updatedctx . mocks . length ;
25
24
const deps = updatedctx . deps . length ;
26
25
const responseBody = await response . text ( ) ;
27
26
const recordedOutput = updatedctx . mocks [ 0 ] . Spec . Res . Body ;
28
- expect ( mockFetch ) . toHaveBeenCalledWith ( url , options ) ;
29
27
expect ( response ) . toBeInstanceOf ( Response ) ;
30
28
expect ( mocks ) . toBeGreaterThan ( 0 ) ;
31
29
expect ( deps ) . toBeGreaterThan ( 0 ) ;
32
30
expect ( response ) . toHaveProperty ( 'body' ) ;
33
31
expect ( responseBody ) . toEqual ( recordedOutput ) ;
34
32
} ) ;
35
33
36
- it ( 'should return mocked response in test mode' , async ( ) => {
34
+ it ( 'should return mocked response in test mode' , async ( ) => {
37
35
const mockResponse = new Response ( 'mocked response' ) ;
38
- const mockFetch = jest . fn ( ) . mockResolvedValue ( mockResponse ) ;
39
36
const ctx = {
40
37
mode : 'test' ,
41
38
testId : 'testId' ,
@@ -47,12 +44,12 @@ describe('wrappedNodeFetch', () => {
47
44
Spec : {
48
45
Metadata : {
49
46
name : 'node-fetch' ,
50
- url : 'http ://example.com ' ,
47
+ url : 'https ://api.keploy.io/healthz ' ,
51
48
options : { method : 'GET' } ,
52
49
type : 'HTTP_CLIENT' ,
53
50
} ,
54
51
Req : {
55
- URL : 'http ://example.com ' ,
52
+ URL : 'https ://api.keploy.io/healthz ' ,
56
53
Body : '' ,
57
54
Header : { } ,
58
55
Method : 'GET' ,
@@ -70,23 +67,25 @@ describe('wrappedNodeFetch', () => {
70
67
} ;
71
68
createExecutionContext ( ctx )
72
69
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 ' ;
75
72
const options = {
76
73
method : 'GET' ,
77
74
} ;
78
75
const response = await wrappedFetch ( url , options ) ;
79
76
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 ;
82
81
expect ( mocks ) . toBe ( 0 ) ;
83
82
} ) ;
84
83
85
84
it ( 'should return undefined if execution context is not present in record mode' , async ( ) => {
86
85
const mockFetch = jest . fn ( ) . mockResolvedValue ( new Response ( ) ) ;
87
86
const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ) ;
88
87
const wrappedFetch = ( wrappedNodeFetch ( mockFetch ) as any ) . bind ( { fetch : mockFetch } ) ;
89
- const url = 'http ://example.com ' ;
88
+ const url = 'https ://api.keploy.io/healthz ' ;
90
89
const options = {
91
90
method : 'GET' ,
92
91
} ;
@@ -106,7 +105,7 @@ describe('wrappedNodeFetch', () => {
106
105
createExecutionContext ( ctx )
107
106
108
107
const wrappedFetch = ( wrappedNodeFetch ( mockFetch ) as any ) . bind ( { fetch : mockFetch } ) ;
109
- const url = 'http ://example.com ' ;
108
+ const url = 'https ://api.keploy.io/healthz ' ;
110
109
const options = {
111
110
method : 'GET' ,
112
111
} ;
0 commit comments