/
test.js
65 lines (52 loc) · 1.62 KB
/
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
/* eslint prefer-destructuring: 0 */
const arrayUnique = require('array-unique');
const server = require('express')();
const portpids = require('.');
describe('expectations', () => {
test('expects a port number', () => {
expect(() => {
portpids({});
}).toThrow();
});
test('returns empty objects on error', async () => portpids(-1).then((pids) => {
expect.assertions(3);
expect(Array.isArray(pids.all) && pids.all.length === 0).toEqual(true);
expect(Array.isArray(pids.tcp) && pids.tcp.length === 0).toEqual(true);
expect(Array.isArray(pids.udp) && pids.udp.length === 0).toEqual(true);
}));
});
describe('gets the pids', () => {
let listener;
let port;
beforeEach(() => {
listener = server.listen(0);
port = listener.address().port;
});
afterEach(() => {
listener.close();
});
test('gets the pids', async () => portpids(port).then((pids) => {
expect.assertions(4);
expect(pids.all.length > 0).toEqual(true);
expect(typeof pids.all[0]).toEqual('number');
expect(pids.tcp.length > 0).toEqual(true);
expect(typeof (pids.tcp[0])).toEqual('number');
}));
});
describe('no duplicates', () => {
let listener;
let port;
beforeEach(() => {
listener = server.listen(0);
port = listener.address().port;
});
afterEach(() => {
listener.close();
});
test('doesnt push duplicate entries', async () => portpids(port).then((pids) => {
expect.assertions(3);
expect(pids.all).toMatchObject(arrayUnique(pids.all));
expect(pids.tcp).toMatchObject(arrayUnique(pids.tcp));
expect(pids.udp).toMatchObject(arrayUnique(pids.udp));
}));
});