forked from josh-berry/tab-stash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-detect-leaks.mjs
69 lines (60 loc) · 1.48 KB
/
test-detect-leaks.mjs
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
// https://gist.github.com/boneskull/7fe75b63d613fa940db7ec990a5f5843
import {createHook} from "async_hooks";
import {stackTraceFilter} from "mocha/lib/utils.js";
const allResources = new Map();
// this will pull Mocha internals out of the stacks
const filterStack = stackTraceFilter();
const hook = createHook({
init(asyncId, type, triggerAsyncId) {
const parent = allResources.get(triggerAsyncId);
const r = {
type,
asyncId,
stack: filterStack(
new Error(`${type} ${asyncId} triggered by ${triggerAsyncId}`).stack,
),
parent,
children: [],
};
allResources.set(asyncId, r);
if (parent) parent.children.push(r);
},
destroy(asyncId) {
allResources.delete(asyncId);
},
}).enable();
const asyncDump = () => {
function print(r) {
let dots = false;
console.error(r.stack);
r = r.parent;
while (r) {
if (r.parent && r.children.length <= 1) {
if (!dots) {
console.error("...");
dots = true;
}
r = r.parent;
continue;
}
print(r);
r = r.parent;
}
}
hook.disable();
console.error(`
STUFF STILL IN THE EVENT LOOP:`);
allResources.forEach(value => {
if (value.children.length !== 0) return;
if (value.type === "Immediate") return;
if (value.type === "PROMISE") return;
// print(value);
console.error(value.stack);
console.error("");
});
};
export const mochaHooks = {
afterAll() {
asyncDump();
},
};