-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathinstrumentation.test.js
112 lines (94 loc) · 3.92 KB
/
instrumentation.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* eslint-env node, jest */
const instrumentation = require("./instrumentation");
test("preload shims Promise.then", () => {
expect(Promise.prototype.then.__wrapped).toBeUndefined();
instrumentation.instrumentPreload();
expect(Promise.prototype.then.__wrapped).toBe(true);
});
let createFakeExpress = () => {
return {};
};
let createFakeChildProcess = () => ({
exec() {},
execFile() {},
});
test("we don't instrument modules if the requesting module is from our package", () => {
instrumentation.clearInstrumentationForTesting();
let fakeExpress = createFakeExpress();
let m = instrumentation.instrumentLoad(fakeExpress, "express", {
id: "foo/stuff/node_modules/honeycomb-beeline/lib/index.js",
});
expect(m).toBe(fakeExpress);
expect(m.__wrapped).toBeUndefined();
});
test("we only instrument once and then cache the result", () => {
instrumentation.clearInstrumentationForTesting();
let fakeExpress = createFakeExpress();
let m = instrumentation.instrumentLoad(fakeExpress, "express", { id: "foo/stuff/node_modules/" });
// we instrument express by returning a different wrapper
expect(m).not.toBe(fakeExpress);
expect(m.__wrapped).toBe(true);
let m2 = instrumentation.instrumentLoad(fakeExpress, "express", {
id: "foo/stuff/node_modules/",
});
// we return the cached result
expect(m2).toBe(m);
});
test("instrumentation is keyed off the requested module name, not the module itself", () => {
instrumentation.clearInstrumentationForTesting();
let fakeExpress = createFakeExpress();
let m = instrumentation.instrumentLoad(fakeExpress, "express2", {
id: "foo/stuff/node_modules/",
});
// we return fakeExpress uninstrumented because we don't recognize the name
expect(m).toBe(fakeExpress);
expect(m.__wrapped).toBeUndefined();
});
test("if an instrumentation is disabled, don't instrument the module", () => {
instrumentation.clearInstrumentationForTesting();
instrumentation.configure({ enabledInstrumentations: [], disableInstrumentationOnLoad: true });
let fakeExpress = createFakeExpress();
let m = instrumentation.instrumentLoad(fakeExpress, "express", {
id: "foo/stuff/node_modules/",
});
expect(m).toBe(fakeExpress);
expect(m.__wrapped).toBeUndefined();
});
test("if an instrumentation is enabled, instrument the module", () => {
instrumentation.clearInstrumentationForTesting();
instrumentation.configure({
enabledInstrumentations: ["express"],
disableInstrumentationOnLoad: true,
});
let fakeExpress = createFakeExpress();
let m = instrumentation.instrumentLoad(fakeExpress, "express", {
id: "foo/stuff/node_modules/",
});
expect(m).not.toBe(fakeExpress);
expect(m.__wrapped).toBe(true);
});
test("we keep track of the active instrumentations in a sorted array", () => {
instrumentation.clearInstrumentationForTesting();
let fakeExpress = createFakeExpress();
let fakeChildProcess = createFakeChildProcess();
expect(instrumentation.activeInstrumentations()).toEqual([]);
// instrument (and make sure we instrumented) express
let m = instrumentation.instrumentLoad(fakeExpress, "express", { id: "foo/stuff/node_modules/" });
expect(m.__wrapped).toBe(true);
// now it should show up in our activeInstrumentations
expect(instrumentation.activeInstrumentations()).toEqual(["express"]);
// instrument (and make sure we instrumented) child_process
let m2 = instrumentation.instrumentLoad(fakeChildProcess, "child_process", {
id: "foo/stuff/node_modules/",
});
expect(m2.execFile.__wrapped).toBe(true);
// activeInstrumentations now includes child_process
expect(instrumentation.activeInstrumentations()).toEqual(["child_process", "express"]);
});
test("don't instrument module if parent is null", () => {
instrumentation.clearInstrumentationForTesting();
let fakeExpress = createFakeExpress();
let m = instrumentation.instrumentLoad(fakeExpress, "express", null);
expect(m).toBe(fakeExpress);
expect(m.__wrapped).toBeUndefined();
});