-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathcontract-example-with-openapi.test.js
85 lines (73 loc) · 2.32 KB
/
contract-example-with-openapi.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
const path = require('path');
const nock = require('nock');
const jestOpenAPI = require('jest-openapi').default;
const {
testSetup,
} = require('../../../example-application/test/setup/test-file-setup');
jestOpenAPI(path.join(__dirname, '../../../example-application/openapi.json'));
beforeAll(async () => {
await testSetup.start({
startAPI: true,
disableNetConnect: true,
includeTokenInHttpClient: true,
mockGetUserCalls: true,
mockMailerCalls: true,
});
});
beforeEach(() => {
testSetup.resetBeforeEach();
});
afterAll(async () => {
// ️️️✅ Best Practice: Clean-up resources after each run
testSetup.tearDownTestFile();
});
describe('Verify openApi (Swagger) spec', () => {
// ️️️✅ Best Practice: When testing the API contract/doc, write a test against each route and potential HTTP status
describe('POST /orders', () => {
test('When added a valid order and 200 was expected', async () => {
nock('http://localhost/user/').get(`/1`).reply(200, {
id: 1,
name: 'John',
});
const orderToAdd = {
userId: 1,
productId: 2,
mode: 'approved',
};
const receivedResponse = await testSetup
.getHTTPClient()
.post('/order', orderToAdd);
// ️️️✅ Best Practice: When testing the API contract/doc
expect(receivedResponse).toSatisfyApiSpec();
});
test('When an invalid order was send, then error 400 is expected', async () => {
// Arrange
nock('http://localhost/user/').get(`/1`).reply(200, {
id: 1,
name: 'John',
});
const orderToAdd = {
userId: 1,
productId: undefined, //❌
mode: 'approved',
};
// Act
const receivedResponse = await testSetup
.getHTTPClient()
.post('/order', orderToAdd);
// Assert
expect(receivedResponse).toSatisfyApiSpec();
expect(receivedResponse.status).toBe(400);
});
test('When a call to the users microservice fails, then get back 404 error', async () => {
nock('http://localhost/user/').get(`/1`).reply(404);
const orderToAdd = {
userId: 1,
productId: 2,
mode: 'approved',
};
const res = await testSetup.getHTTPClient().post('/order', orderToAdd);
expect(res).toSatisfyApiSpec();
});
});
});