1
1
import { wrappedNodeFetch } from '../integrations/octokit/require' ;
2
2
import { Response } from 'node-fetch' ;
3
3
import fetch from 'node-fetch' ;
4
- import { createExecutionContext , getExecutionContext } from '../src/context' ;
4
+ import { createExecutionContext , getExecutionContext } from '../src/context' ;
5
5
import { HTTP } from '../src/keploy' ;
6
6
7
7
describe ( 'wrappedNodeFetch' , ( ) => {
8
- it ( 'should return mocked response in test mode - case 1' , async ( ) => {
9
- const mockResponse = new Response ( 'mocked response' ) ;
8
+ it ( 'should call fetch function with correct arguments in record mode' , async ( ) => {
10
9
const ctx = {
11
- mode : 'test ' ,
10
+ mode : 'record ' ,
12
11
testId : 'testId' ,
13
- mocks : [
14
- {
15
- Version : 'V1_BETA2' ,
16
- Name : 'testId' ,
17
- Kind : HTTP ,
18
- Spec : {
19
- Metadata : {
20
- name : 'node-fetch' ,
21
- url : 'https://api.keploy.io/healthz' ,
22
- options : { method : 'GET' } ,
23
- type : 'HTTP_CLIENT' ,
24
- } ,
25
- Req : {
26
- URL : 'https://api.keploy.io/healthz' ,
27
- Body : '' ,
28
- Header : { } ,
29
- Method : 'GET' ,
30
- } ,
31
- Res : {
32
- StatusCode : 200 ,
33
- Header : { 'content-type' : { Value : [ 'text/plain' ] } } ,
34
- Body : 'mocked response' ,
35
- } ,
36
- } ,
37
- } ,
38
- ] ,
39
- deps : [ ] ,
40
-
12
+ mocks : [ ] ,
13
+ deps : [ ] ,
41
14
} ;
42
15
createExecutionContext ( ctx )
43
-
44
16
const wrappedFetch = ( wrappedNodeFetch ( fetch ) as any ) . bind ( { fetch } ) ;
45
17
const url = 'https://api.keploy.io/healthz' ;
46
18
const options = {
47
19
method : 'GET' ,
48
20
} ;
49
21
const response = await wrappedFetch ( url , options ) ;
50
22
const updatedctx = getExecutionContext ( ) . context ;
51
- expect ( response . status ) . toEqual ( mockResponse . status ) ;
52
- expect ( response . statusText ) . toEqual ( mockResponse . statusText ) ;
53
-
54
- const mocks = updatedctx . mocks . length ;
55
- expect ( mocks ) . toBe ( 0 ) ;
23
+ const responseBody = await response . text ( ) ;
24
+ const recordedOutput = updatedctx . mocks [ 0 ] . Spec . Res . Body ;
25
+ expect ( response ) . toBeInstanceOf ( Response ) ;
26
+ expect ( updatedctx . mocks . length ) . toBeGreaterThan ( 0 ) ;
27
+ expect ( updatedctx . deps . length ) . toBeGreaterThan ( 0 ) ;
28
+ expect ( response ) . toHaveProperty ( 'body' ) ;
29
+ expect ( responseBody ) . toEqual ( recordedOutput ) ;
56
30
} ) ;
57
31
58
- it ( 'should return mocked response in test mode - case 2 ' , async ( ) => {
32
+ it ( 'should return mocked response in test mode' , async ( ) => {
59
33
const mockResponse = new Response ( 'mocked response' ) ;
60
34
const ctx = {
61
35
mode : 'test' ,
@@ -68,12 +42,12 @@ describe('wrappedNodeFetch', () => {
68
42
Spec : {
69
43
Metadata : {
70
44
name : 'node-fetch' ,
71
- url : 'https://api.keploy.io/status ' ,
45
+ url : 'https://api.keploy.io/healthz ' ,
72
46
options : { method : 'GET' } ,
73
47
type : 'HTTP_CLIENT' ,
74
48
} ,
75
49
Req : {
76
- URL : 'https://api.keploy.io/status ' ,
50
+ URL : 'https://api.keploy.io/healthz ' ,
77
51
Body : '' ,
78
52
Header : { } ,
79
53
Method : 'GET' ,
@@ -92,7 +66,7 @@ describe('wrappedNodeFetch', () => {
92
66
createExecutionContext ( ctx )
93
67
94
68
const wrappedFetch = ( wrappedNodeFetch ( fetch ) as any ) . bind ( { fetch } ) ;
95
- const url = 'https://api.keploy.io/status ' ;
69
+ const url = 'https://api.keploy.io/healthz ' ;
96
70
const options = {
97
71
method : 'GET' ,
98
72
} ;
@@ -104,52 +78,20 @@ describe('wrappedNodeFetch', () => {
104
78
const mocks = updatedctx . mocks . length ;
105
79
expect ( mocks ) . toBe ( 0 ) ;
106
80
} ) ;
107
- it ( 'should record an HTTP request in record mode' , async ( ) => {
108
- const ctx = {
109
- mode : 'record' ,
110
- testId : 'testId' ,
111
- mocks : [ ] ,
112
- deps : [ ] ,
113
- } ;
114
- createExecutionContext ( ctx )
115
- const wrappedFetch = ( wrappedNodeFetch ( fetch ) as any ) . bind ( { fetch } ) ;
116
- const url = 'https://api.example.com/test' ;
117
- const options = {
118
- method : 'POST' ,
119
- headers : {
120
- 'Content-Type' : 'application/json' ,
121
- } ,
122
- body : JSON . stringify ( { data : 'test' } ) ,
123
- } ;
124
- const response = await wrappedFetch ( url , options ) ;
125
- const updatedctx = getExecutionContext ( ) . context ;
126
- const responseBody = await response . text ( ) ;
127
- const recordedOutput = updatedctx . mocks [ 0 ] . Spec . Res . Body ;
128
- expect ( response ) . toBeInstanceOf ( Response ) ;
129
- expect ( updatedctx . mocks . length ) . toBeGreaterThan ( 0 ) ;
130
- expect ( updatedctx . deps . length ) . toBeGreaterThan ( 0 ) ;
131
- expect ( response ) . toHaveProperty ( 'body' ) ;
132
- expect ( responseBody ) . toEqual ( recordedOutput ) ;
133
- } ) ;
134
81
135
- it ( 'should handle an HTTP error in record mode' , async ( ) => {
136
- const ctx = {
137
- mode : 'record' ,
138
- testId : 'testId' ,
139
- mocks : [ ] ,
140
- deps : [ ] ,
141
- } ;
142
- createExecutionContext ( ctx )
143
- const wrappedFetch = ( wrappedNodeFetch ( fetch ) as any ) . bind ( { fetch } ) ;
144
- const url = 'https://api.example.com/error' ;
82
+ it ( 'should return undefined if execution context is not present in record mode' , async ( ) => {
83
+ const mockFetch = jest . fn ( ) . mockResolvedValue ( new Response ( ) ) ;
84
+ const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ) ;
85
+ const wrappedFetch = ( wrappedNodeFetch ( mockFetch ) as any ) . bind ( { fetch : mockFetch } ) ;
86
+ const url = 'https://api.keploy.io/healthz' ;
145
87
const options = {
146
88
method : 'GET' ,
147
89
} ;
148
- await expect ( wrappedFetch ( url , options ) ) . rejects . toThrow ( 'Not Found' ) ;
149
- const updatedctx = getExecutionContext ( ) . context ;
150
- expect ( updatedctx . mocks . length ) . toBeGreaterThan ( 0 ) ;
151
- expect ( updatedctx . deps . length ) . toBeGreaterThan ( 0 ) ;
90
+ const response = await wrappedFetch ( url , options ) ;
91
+ expect ( consoleSpy ) . toHaveBeenCalledWith ( 'keploy context is not present to mock dependencies' ) ;
92
+ expect ( response ) . toBeUndefined ( ) ;
152
93
} ) ;
94
+
153
95
it ( 'should call fetch function with correct arguments in off mode' , async ( ) => {
154
96
const mockFetch = jest . fn ( ) . mockResolvedValueOnce ( new Response ( ) ) ;
155
97
const ctx = {
@@ -170,49 +112,4 @@ describe('wrappedNodeFetch', () => {
170
112
expect ( mockFetch ) . toHaveBeenCalledWith ( url , options ) ;
171
113
expect ( response ) . toBeInstanceOf ( Response ) ;
172
114
} ) ;
173
-
174
- it ( 'should return an error if fetch is called in off mode with a mock' , async ( ) => {
175
- const mockFetch = jest . fn ( ) . mockResolvedValue ( new Response ( ) ) ;
176
- const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ) ;
177
- const ctx = {
178
- context : 'off' ,
179
- testId : 'testId' ,
180
- mocks : [
181
- {
182
- Version : 'V1_BETA2' ,
183
- Name : 'testId' ,
184
- Kind : HTTP ,
185
- Spec : {
186
- Metadata : {
187
- name : 'node-fetch' ,
188
- url : 'https://api.keploy.io/healthz' ,
189
- options : { method : 'GET' } ,
190
- type : 'HTTP_CLIENT' ,
191
- } ,
192
- Req : {
193
- URL : 'https://api.keploy.io/healthz' ,
194
- Body : '' ,
195
- Header : { } ,
196
- Method : 'GET' ,
197
- } ,
198
- Res : {
199
- StatusCode : 200 ,
200
- Header : { 'content-type' : { Value : [ 'text/plain' ] } } ,
201
- Body : 'mocked response' ,
202
- } ,
203
- } ,
204
- } ,
205
- ] ,
206
- deps : [ ] ,
207
- } ;
208
- createExecutionContext ( ctx )
209
- const wrappedFetch = ( wrappedNodeFetch ( mockFetch ) as any ) . bind ( { fetch : mockFetch } ) ;
210
- const url = 'https://api.keploy.io/healthz' ;
211
- const options = {
212
- method : 'GET' ,
213
- } ;
214
- const response = await wrappedFetch ( url , options ) ;
215
- expect ( consoleSpy ) . toHaveBeenCalledWith ( 'Cannot mock dependencies in off mode' ) ;
216
- expect ( response ) . toBeUndefined ( ) ;
217
- } ) ;
218
- } ) ;
115
+ } ) ;
0 commit comments