-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
95 lines (84 loc) · 2.73 KB
/
index.test.js
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
import { expect } from 'chai';
import { like } from 'pactum-matchers';
import MockServer from './index.js';
let I;
let restClient;
const port = 65000;
const api_url = `http://0.0.0.0:${port}`;
describe('MockServer Helper', () => {
beforeAll(() => {
I = new MockServer({ port });
});
beforeEach(async () => {
await I.startMockServer();
});
afterEach(async () => {
await I.stopMockServer();
});
describe('#startMockServer', () => {
it('should start the mock server with custom port', async () => {
global.debugMode = true;
await I.startMockServer(6789);
await I.stopMockServer();
global.debugMode = undefined;
});
});
describe('#addInteractionToMockServer', () => {
it('should return the correct response', async () => {
await I.addInteractionToMockServer({
request: {
method: 'GET',
path: '/api/hello',
},
response: {
status: 200,
body: {
say: 'hello to mock server',
},
},
});
const res = await fetch(api_url + '/api/hello');
expect(await res.json()).to.eql({ say: 'hello to mock server' });
});
it('should return 404 when not found route', async () => {
const res = await fetch(api_url + '/api/notfound');
expect(res.status).to.eql(404);
});
it('should return the strong match on query parameters', async () => {
await I.addInteractionToMockServer({
request: {
method: 'GET',
path: '/api/users',
queryParams: {
id: 1,
},
},
response: {
status: 200,
body: {
user: 1,
},
},
});
await I.addInteractionToMockServer({
request: {
method: 'GET',
path: '/api/users',
queryParams: {
id: 2,
},
},
response: {
status: 200,
body: {
user: 2,
},
},
});
let res = await fetch(api_url + '/api/users?id=1');
expect(await res.json()).to.eql({ user: 1 });
res = await fetch(api_url + '/api/users?id=2');
expect(await res.json()).to.eql({ user: 2 });
});
});
});