forked from marcominetti/node-memwatch
-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.test.js
92 lines (85 loc) · 2.89 KB
/
main.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
86
87
88
89
90
91
import { describe, it, expect } from 'vitest';
import memwatch from './include';
describe('node-memwatch', () => {
it('The library exports a couple functions', () => {
expect(memwatch.gc).toBeDefined();
expect(memwatch.on).toBeDefined();
expect(memwatch.once).toBeDefined();
expect(memwatch.removeAllListeners).toBeDefined();
expect(memwatch.HeapDiff).toBeDefined();
});
describe('.gc()', () => {
it('causes a stats() event to be emitted', () => {
return new Promise((resolve) => {
memwatch.once('stats', (s) => {
expect(typeof s).toBe('object');
resolve();
});
memwatch.gc();
});
});
});
describe('HeapDiff', () => {
it('detects allocations', () => {
function LeakingClass() {};
const arr = [];
const hd = new memwatch.HeapDiff();
for (let i = 0; i < 100; i++) arr.push(new LeakingClass());
const diff = hd.end();
expect(Array.isArray(diff.change.details)).toBe(true);
// find the LeakingClass elem
let leakingReport;
diff.change.details.forEach((d) => {
if (d.what === 'LeakingClass')
leakingReport = d;
});
expect(leakingReport).toBeDefined();
expect(leakingReport['+'] - leakingReport['-']).toBeGreaterThan(0);
});
it('has the correct output format', () => {
function LeakingClass() {};
const arr = [];
const hd = new memwatch.HeapDiff();
for (let i = 0; i < 100; i++) arr.push(new LeakingClass());
expect(hd.end()).toEqual(
expect.objectContaining({
after: expect.objectContaining({
nodes: expect.any(Number),
size: expect.any(String),
size_bytes: expect.any(Number),
}),
before: expect.objectContaining({
nodes: expect.any(Number),
size: expect.any(String),
size_bytes: expect.any(Number),
}),
change: expect.objectContaining({
allocated_nodes: expect.any(Number),
details: expect.arrayContaining([
expect.objectContaining({
'+': expect.any(Number),
'-': expect.any(Number),
size: expect.any(String),
size_bytes: expect.any(Number),
what: expect.any(String),
}),
]),
freed_nodes: expect.any(Number),
size: expect.any(String),
size_bytes: expect.any(Number),
}),
})
)
});
it('double end should throw', () => {
const hd = new memwatch.HeapDiff();
expect(() => hd.end()).not.toThrow();
expect(() => hd.end()).toThrow();
});
it('improper HeapDiff allocation should throw an exception', () => {
// equivalent to "new require('memwatch').HeapDiff()"
// see issue #30
expect(() => new (memwatch.HeapDiff())).toThrow();
});
});
});