-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.js
48 lines (44 loc) · 1.37 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
const request = require('superagent');
const logNetworkTime = require('./index');
const https = require('https')
jest.setTimeout(10000);
const httpAgent = new https.Agent({keepAlive: true});
describe('GIVEN plugin was used WHEN response arrives THEN callback should get timings information', () => {
test('timings', () => {
const assertions = [
'verified that timings were populated'
];
expect.assertions(assertions.length);
return expect(makeRequest()).resolves.toMatchSnapshot();
});
test('works with reusable keepAlive agent, we simply call the same request again with the same agent', () => {
const assertions = [
'verified that timings were populated'
];
expect.assertions(assertions.length);
return expect(makeRequest()).resolves.toMatchSnapshot();
})
function makeRequest() {
return new Promise((res, rej) => {
request
.get(`https://google.com`)
.use(logNetworkTime((err, result) => {
if (err) {
return rej(err)
}
res(valuesToTypes(result))
}))
.agent(httpAgent)
.then(x => x)
.catch(rej)
})
}
});
function valuesToTypes(obj) {
return Object.keys(obj).reduce((acc, key) => {
const value = obj[key];
const type = typeof value;
acc[key] = type === 'object' ? valuesToTypes(value) : type;
return acc;
}, {})
}