-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathaudits.test.ts
227 lines (211 loc) · 5.55 KB
/
audits.test.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { describe, it, expect } from 'vitest';
import {
Audit,
serverAudits,
AuditResult,
renderAuditResultsToHTML,
AuditFail,
} from '../src/audits';
import htmlValidator from 'html-validator';
it('should have globally unique audit ids', () => {
const ids: string[] = [];
for (const audit of serverAudits({
url: 'http://localhost',
fetchFn: () => {
// noop
},
})) {
expect(ids).not.toContain(audit.id);
ids.push(audit.id);
}
});
it('should not change globally unique audit ids', () => {
const audits: Omit<Audit, 'fn'>[] = [];
serverAudits({
url: 'http://localhost',
fetchFn: () => {
// noop
},
}).forEach(({ fn, ...audit }) => audits.push(audit));
// update snapshot if new audits are added or deleted,
// but existing ones SHOULD NOT CHANGE semantically
expect(audits).toMatchSnapshot();
});
it('should allow re-reading the response body in results', async () => {
const body = '{ "errors": [{ "message": "hello" }] }';
const audit = serverAudits({
url: 'http://localhost',
fetchFn: () => new Response(body),
}).find(
({ id }) =>
// test itself is not important - we just need one that reads the body
id === '13EE',
);
if (!audit) {
throw new Error('Expected audit not found');
}
const result = await audit.fn();
expect(result.status).toBe('error');
await expect((result as AuditFail).response.text()).resolves.toBe(body);
});
describe('Render audit results to HTML', () => {
const results: AuditResult[] = [
{
id: 'ok1',
name: 'MUST ok1',
status: 'ok',
},
{
id: 'ok2',
name: 'MUST ok2',
status: 'ok',
},
{
id: 'warn1',
name: 'SHOULD warn1',
status: 'warn',
reason: 'bad warn1',
response: new Response('Warning!', {
status: 400,
headers: { 'x-id': 'warn1' },
}),
},
{
id: 'warn2',
name: 'SHOULD warn2',
status: 'warn',
reason: 'bad warn2',
response: new Response('Warning!', {
status: 400,
headers: { 'x-id': 'warn2' },
}),
},
{
id: 'error1',
name: 'MUST error1',
status: 'error',
reason: 'bad error1',
response: new Response('Error!', {
status: 500,
headers: { 'x-id': 'error1' },
}),
},
{
id: 'error2',
name: 'MUST error2',
status: 'error',
reason: 'bad error2',
response: new Response('Error!', {
status: 500,
headers: { 'x-id': 'error2' },
}),
},
];
it('should render HTML', async () => {
await expect(renderAuditResultsToHTML(results)).resolves
.toMatchInlineSnapshot(`
"<i>* This report was auto-generated by graphql-http</i>
<h1>GraphQL over HTTP audit report</h1>
<ul>
<li><b>6</b> audits in total</li>
<li><span style=\\"font-family: monospace\\">✅</span> <b>2</b> pass</li>
<li><span style=\\"font-family: monospace\\">⚠️</span> <b>2</b> warnings (optional)</li>
<li><span style=\\"font-family: monospace\\">❌</span> <b>2</b> errors (required)</li>
</ul>
<h2>Passing</h2>
<ol>
<li><code>ok1</code> MUST ok1</li>
<li><code>ok2</code> MUST ok2</li>
</ol>
<h2>Warnings</h2>
The server <i>SHOULD</i> support these, but is not required.
<ol>
<li><code>warn1</code> SHOULD warn1
<details>
<summary>bad warn1</summary>
<pre><code class=\\"lang-json\\">{
\\"statusText\\": \\"\\",
\\"status\\": 400,
\\"headers\\": {
\\"x-id\\": \\"warn1\\",
\\"content-type\\": \\"text/plain;charset=UTF-8\\"
},
\\"body\\": \\"Warning!\\"
}
</code></pre>
</details>
</li>
<li><code>warn2</code> SHOULD warn2
<details>
<summary>bad warn2</summary>
<pre><code class=\\"lang-json\\">{
\\"statusText\\": \\"\\",
\\"status\\": 400,
\\"headers\\": {
\\"x-id\\": \\"warn2\\",
\\"content-type\\": \\"text/plain;charset=UTF-8\\"
},
\\"body\\": \\"Warning!\\"
}
</code></pre>
</details>
</li>
</ol>
<h2>Errors</h2>
The server <b>MUST</b> support these.
<ol>
<li><code>error1</code> MUST error1
<details>
<summary>bad error1</summary>
<pre><code class=\\"lang-json\\">{
\\"statusText\\": \\"\\",
\\"status\\": 500,
\\"headers\\": {
\\"x-id\\": \\"error1\\",
\\"content-type\\": \\"text/plain;charset=UTF-8\\"
},
\\"body\\": \\"Error!\\"
}
</code></pre>
</details>
</li>
<li><code>error2</code> MUST error2
<details>
<summary>bad error2</summary>
<pre><code class=\\"lang-json\\">{
\\"statusText\\": \\"\\",
\\"status\\": 500,
\\"headers\\": {
\\"x-id\\": \\"error2\\",
\\"content-type\\": \\"text/plain;charset=UTF-8\\"
},
\\"body\\": \\"Error!\\"
}
</code></pre>
</details>
</li>
</ol>
"
`);
});
it('should render well-formatted and valid HTML', async () => {
const rendered = await renderAuditResultsToHTML(results);
const document = `<!DOCTYPE html>
<html lang="en">
<head>
<title>graphql-http</title>
</head>
<body>${rendered}</body>
</html>
`;
await expect(
htmlValidator({
data: document,
}),
).resolves.toMatchInlineSnapshot(`
{
"messages": [],
}
`);
});
});