1
1
import { wrappedNodeFetch } from '../integrations/octokit/require' ;
2
2
import { Response } from 'node-fetch' ;
3
- import { createExecutionContext } from '../src/context' ;
3
+ import { createExecutionContext , getExecutionContext } from '../src/context' ;
4
4
describe ( 'wrappedNodeFetch' , ( ) => {
5
5
it ( 'should call fetch function with correct arguments in record mode' , async ( ) => {
6
6
const mockFetch = jest . fn ( ) . mockResolvedValueOnce ( new Response ( ) ) ;
7
7
const ctx = {
8
- context :{
9
8
mode : 'record' ,
10
9
testId : 'testId' ,
11
10
mocks : [ ] ,
12
- deps : [ ] ,
13
- }
11
+ deps : [ ] ,
14
12
} ;
15
13
createExecutionContext ( ctx )
16
14
const wrappedFetch = ( wrappedNodeFetch ( mockFetch ) as any ) . bind ( { fetch : mockFetch } ) ;
17
15
const url = 'http://example.com' ;
18
16
const options = {
19
17
method : 'GET' ,
20
18
} ;
19
+
21
20
const response = await wrappedFetch ( url , options ) ;
21
+ const updatedctx = getExecutionContext ( ) . context ;
22
+ const mocks = updatedctx . mocks . length ;
23
+ const deps = updatedctx . deps . length ;
22
24
expect ( mockFetch ) . toHaveBeenCalledWith ( url , options ) ;
23
25
expect ( response ) . toBeInstanceOf ( Response ) ;
26
+ expect ( mocks ) . toBeGreaterThan ( 0 ) ;
27
+ expect ( deps ) . toBeGreaterThan ( 0 ) ;
24
28
} ) ;
25
29
26
30
it ( 'should return mocked response in test mode' , async ( ) => {
27
31
const mockResponse = new Response ( 'mocked response' ) ;
28
32
const mockFetch = jest . fn ( ) . mockResolvedValue ( mockResponse ) ;
29
33
const ctx = {
30
- context :{
31
34
mode : 'test' ,
32
35
testId : 'testId' ,
33
36
mocks : [
@@ -38,12 +41,12 @@ describe('wrappedNodeFetch', () => {
38
41
Spec : {
39
42
Metadata : {
40
43
name : 'node-fetch' ,
41
- url : 'http://example.com ' ,
44
+ url : 'http://localhost:8080 ' ,
42
45
options : { method : 'GET' } ,
43
46
type : 'HTTP_CLIENT' ,
44
47
} ,
45
48
Req : {
46
- URL : 'http://example.com ' ,
49
+ URL : 'http://localhost:8080 ' ,
47
50
Body : '' ,
48
51
Header : { } ,
49
52
Method : 'GET' ,
@@ -57,7 +60,7 @@ describe('wrappedNodeFetch', () => {
57
60
} ,
58
61
] ,
59
62
deps : [ ] ,
60
- }
63
+
61
64
} ;
62
65
createExecutionContext ( ctx )
63
66
@@ -67,7 +70,10 @@ describe('wrappedNodeFetch', () => {
67
70
method : 'GET' ,
68
71
} ;
69
72
const response = await wrappedFetch ( url , options ) ;
73
+ const updatedctx = getExecutionContext ( ) . context ;
70
74
expect ( response ) . toEqual ( mockResponse ) ;
75
+ const mocks = updatedctx . mocks . length ( ) ;
76
+ expect ( mocks ) . toBe ( 0 ) ;
71
77
} ) ;
72
78
73
79
it ( 'should return undefined if execution context is not present in record mode' , async ( ) => {
@@ -86,12 +92,10 @@ describe('wrappedNodeFetch', () => {
86
92
it ( 'should call fetch function with correct arguments in off mode' , async ( ) => {
87
93
const mockFetch = jest . fn ( ) . mockResolvedValueOnce ( new Response ( ) ) ;
88
94
const ctx = {
89
- context :{
90
95
context : 'off' ,
91
96
testId : 'testId' ,
92
97
mocks : [ ] ,
93
98
deps : [ ] ,
94
- }
95
99
} ;
96
100
createExecutionContext ( ctx )
97
101
0 commit comments