/
testing.spec.ts
136 lines (118 loc) · 3.89 KB
/
testing.spec.ts
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { createMarble } from './server.spec-setup';
import { ApiCollection } from '../apiCollection';
import { createTestApi } from '../testApi';
import { EndpointData } from '../testing.types';
import { HttpMethodType, HttpStatus } from '@marblejs/core';
import { ApiDocument } from '../apiDocument';
import OpenAPIValidator from 'openapi-schema-validator';
describe('@marblejs/testing - TestApi', () => {
const httpListener = createMarble();
test('starts and finished correctly', async () => {
const testApi = createTestApi({
httpListener,
});
testApi.finish();
});
test('returns response with pre-parsed body from server', async () => {
const testApi = createTestApi({
httpListener,
});
const response = await testApi.get('/people/cathy')
.send();
expect(response.path).toBe('/people/:name');
expect(response.method).toBe('GET');
expect(response.failed).toBe(false);
expect(response.succeeded).toBe(true);
expect(response.status).toBe(HttpStatus.OK);
expect(response.body).toEqual({
firstName: 'Cathy',
lastName: 'Scott',
email: 'cathy.scott98@example.com',
birthday: '8/6/1978',
address: '2059 W Craig Rd',
phone: '(700)-354-2326',
});
expect(response.headers).toMatchObject({
'content-type': ['application/json'],
});
testApi.finish();
});
test('has all basic request methods', () => {
const testApi = createTestApi({
httpListener,
});
testApi.finish();
// Contains all HTTP methods supported by Marble.js (without *)
const methods = Object.keys(HttpMethodType)
.filter(method => isNaN(parseInt(method)) && method !== '*')
.map(method => method.toLowerCase());
for (const method of methods) {
try {
expect(() => testApi[method]('/')).not.toThrow();
} catch {
throw new Error(`Method ${method} does not exist`);
}
}
});
test('stores request in collection', async () => {
const testApi = createTestApi({
httpListener,
}).useHeaders({
'Content-Type': 'application/json',
});
const collection = new ApiCollection('API');
const responseOk = await testApi.get('/people/cathy')
.send();
responseOk.collect(collection);
const responseNotFound = await testApi.get('/people/john')
.send();
responseNotFound.collect(collection);
testApi.finish();
expect(collection.endpoints).toHaveLength(1);
expect(collection.endpoints[0].data).toEqual({
path: '/people/:name',
method: 'GET',
responses: [responseOk.data, responseNotFound.data],
} as EndpointData);
});
test('generates valid OpenAPI 3 documentation based on test requests', async () => {
const testApi = createTestApi({
httpListener,
})
.useHeaders({
'Content-Type': 'application/json',
});
const peopleCollection = new ApiCollection('People');
const notesCollection = new ApiCollection('Notes');
const doc = new ApiDocument('Test API', '0.1.0')
.useServers([
{ url: 'https://example.marblejs.com/api/v1', description: 'Production environment' },
{ url: 'https://example.staging.marblejs.com/api/v1', description: 'Staging environment' },
])
.add(peopleCollection)
.add(notesCollection);
(
await testApi.get('/people/cathy')
.send()
).collect(peopleCollection);
(
await testApi.get('/people/john')
.send()
).collect(peopleCollection);
(
await testApi.post('/notes')
.withBody({
author: 'Cathy',
date: '2019-06-11T09:30:00.000Z',
note: 'This is a sample note.'
})
.send()
).collect(notesCollection);
testApi.finish();
const openApiDoc = doc.generate();
const validator = new OpenAPIValidator({
version: '3.0',
});
expect(validator.validate(openApiDoc).errors).toEqual([]);
});
});