-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathwrappedNodeFetch.test.ts
More file actions
115 lines (106 loc) · 3.78 KB
/
wrappedNodeFetch.test.ts
File metadata and controls
115 lines (106 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { wrappedNodeFetch } from '../integrations/octokit/require';
import { Response } from 'node-fetch';
import fetch from 'node-fetch';
import { createExecutionContext, getExecutionContext} from '../src/context';
import { HTTP } from '../src/keploy';
describe('wrappedNodeFetch', () => {
it('should call fetch function with correct arguments in record mode', async () => {
const ctx = {
mode: 'record',
testId: 'testId',
mocks: [],
deps: [],
};
createExecutionContext(ctx)
const wrappedFetch = (wrappedNodeFetch(fetch) as any).bind({ fetch });
const url = 'https://api.keploy.io/healthz';
const options = {
method: 'GET',
};
const response = await wrappedFetch(url, options);
const updatedctx= getExecutionContext().context;
const responseBody = await response.text();
const recordedOutput = updatedctx.mocks[0].Spec.Res.Body;
expect(response).toBeInstanceOf(Response);
expect(updatedctx.mocks.length).toBeGreaterThan(0);
expect(updatedctx.deps.length).toBeGreaterThan(0);
expect(response).toHaveProperty('body');
expect(responseBody).toEqual(recordedOutput);
});
it('should return mocked response in test mode', async () => {
const mockResponse = new Response('mocked response');
const ctx = {
mode: 'test',
testId: 'testId',
mocks: [
{
Version: 'V1_BETA2',
Name: 'testId',
Kind: HTTP,
Spec: {
Metadata: {
name: 'node-fetch',
url: 'https://api.keploy.io/healthz',
options: { method: 'GET' },
type: 'HTTP_CLIENT',
},
Req: {
URL: 'https://api.keploy.io/healthz',
Body: '',
Header: {},
Method: 'GET',
},
Res: {
StatusCode: 200,
Header: { 'content-type': { Value: ['text/plain'] } },
Body: 'mocked response',
},
},
},
],
deps: [],
};
createExecutionContext(ctx)
const wrappedFetch = (wrappedNodeFetch(fetch) as any).bind({ fetch });
const url = 'https://api.keploy.io/healthz';
const options = {
method: 'GET',
};
const response = await wrappedFetch(url, options);
const updatedctx= getExecutionContext().context;
expect(response.status).toEqual(mockResponse.status);
expect(response.statusText).toEqual(mockResponse.statusText);
const mocks=updatedctx.mocks.length;
expect(mocks).toBe(0);
});
it('should return undefined if execution context is not present in record mode', async () => {
const mockFetch = jest.fn().mockResolvedValue(new Response());
const consoleSpy = jest.spyOn(console, 'error').mockImplementation();
const wrappedFetch = (wrappedNodeFetch(mockFetch) as any).bind({ fetch: mockFetch });
const url = 'https://api.keploy.io/healthz';
const options = {
method: 'GET',
};
const response = await wrappedFetch(url, options);
expect(consoleSpy).toHaveBeenCalledWith('keploy context is not present to mock dependencies');
expect(response).toBeUndefined();
});
it('should call fetch function with correct arguments in off mode', async () => {
const mockFetch = jest.fn().mockResolvedValueOnce(new Response());
const ctx = {
context: 'off',
testId: 'testId',
mocks: [],
deps: [],
};
createExecutionContext(ctx)
const wrappedFetch = (wrappedNodeFetch(mockFetch) as any).bind({ fetch: mockFetch });
const url = 'https://api.keploy.io/healthz';
const options = {
method: 'GET',
};
const response = await wrappedFetch(url, options);
expect(mockFetch).toHaveBeenCalledWith(url, options);
expect(response).toBeInstanceOf(Response);
});
});