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
|
"use strict"; // run code in ES5 strict mode
var path = require("path"),
expect = require("expect.js"),
trick = require("../lib/index.js");
var testModules = [
path.resolve(__dirname, "./testModules/index.js"),
path.resolve(__dirname, "./testModules/A/moduleA.js"),
path.resolve(__dirname, "./testModules/B/moduleB.js"),
path.resolve(__dirname, "./testModules/C/moduleC.js")
];
function cleanRequireCache() {
var i;
for (i = 0; i < testModules.length; i++) {
delete require.cache[testModules[i]];
}
}
describe("#trick", function () {
beforeEach(cleanRequireCache); // ensuring a clean test environment
it("should work like require() when omitting all other params", function () {
expect(trick("./testModules/A/moduleA.js")).to.be(require("./testModules/A/moduleA.js"));
});
it("should require all mocks", function () {
var tricked,
fsMock = {},
mocks = {},
moduleBMock = {},
moduleCMock = {},
toSrcMock = {},
indexMock = {};
mocks["fs"] = fsMock;
mocks[path.resolve(__dirname, "./testModules/B/moduleB.js")] = moduleBMock;
mocks["../C/moduleC.js"] = moduleCMock;
mocks["toSrc"] = toSrcMock;
mocks["../"] = indexMock;
tricked = trick("./testModules/A/moduleA.js", mocks);
expect(tricked.fs).to.be(fsMock);
expect(tricked.b).to.be(moduleBMock);
expect(tricked.c).to.be(moduleCMock);
expect(tricked.toSrc).to.be(toSrcMock);
expect(tricked.index).to.be(indexMock);
});
it("should inject object modifications", function () {
var tricked,
injections = {
process: {
argv: ["arg1", "arg2", "arg3"]
},
console: 123
};
tricked = trick("./testModules/A/moduleA.js", null, injections);
expect(tricked.process).not.to.be(process);
expect(process.argv).not.to.eql(injections.process.argv);
expect(tricked.process).to.eql(injections.process);
expect(tricked.console).to.be(123);
});
it("should inject custom scripts", function () {
var tricked,
script = "var console = 456;";
tricked = trick("./testModules/A/moduleA.js", null, script);
expect(tricked.console).to.be(456);
});
it("should leak private variables", function () {
var tricked,
leaks = ["myPrivateVar"];
tricked = trick("./testModules/A/moduleA.js", null, null, leaks);
expect(tricked.__.myPrivateVar).to.be("Hello I'm very private");
});
it("should leak nothing on demand", function () {
var tricked;
tricked = trick("./testModules/A/moduleA.js");
expect(tricked.__).to.be(undefined);
});
it("should cache the tricked module", function () {
var tricked;
tricked = trick("./testModules/B/moduleB.js");
tricked.requireIndex();
expect(tricked.index.b).to.be(tricked);
cleanRequireCache();
tricked = trick("./testModules/B/moduleB.js", null, null, null, true);
tricked.requireIndex();
expect(tricked.index.b).to.be(tricked);
});
it("should not cache the tricked module on demand", function () {
var tricked;
tricked = trick("./testModules/B/moduleB.js", null, null, null, false);
tricked.requireIndex();
expect(tricked.index.b).not.to.be(tricked);
});
});
|