Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4eea33

Browse files
committed
changed injection behaviour
1 parent 07496ad commit d4eea33

File tree

6 files changed

+57
-67
lines changed

6 files changed

+57
-67
lines changed

lib/getInjectionSrc.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"use strict"; // run code in ES5 strict mode
2+
3+
var toSrc = require("toSrc");
4+
5+
function getMonkeyPatchSrc(obj) {
6+
function walkObj(obj, level) {
7+
var key,
8+
value,
9+
src = "";
10+
11+
for (key in obj) {
12+
if (obj.hasOwnProperty(key)) {
13+
value = obj[key];
14+
if (level === 0) {
15+
src += "var "; // on the top level, we need a var statement to override variables
16+
}
17+
src += key + "=" + toSrc(value, 9999) + ";";
18+
}
19+
}
20+
21+
22+
return src;
23+
}
24+
25+
return walkObj(obj, 0);
26+
}
27+
28+
module.exports = getMonkeyPatchSrc;

lib/getMonkeyPatchSrc.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

lib/trick.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var Module = require("module"),
44
nodeWrapper0 = Module.wrapper[0], // caching original wrapper
55
nodeWrapper1 = Module.wrapper[1],
66
getLeakingSrc = require("./getLeakingSrc.js"),
7-
getMonkeyPatchSrc = require("./getMonkeyPatchSrc.js");
7+
getInjectionSrc = require("./getInjectionSrc.js");
88

99
function restoreOriginalWrappers() {
1010
Module.wrapper[0] = nodeWrapper0;
@@ -37,7 +37,7 @@ function trick(parentModule, filename, mocks, injections, leaks, cache) {
3737

3838
// Prepare module for injection
3939
if (typeof injections === "object") {
40-
Module.wrapper[0] = nodeWrapper0 + getMonkeyPatchSrc(injections);
40+
Module.wrapper[0] = nodeWrapper0 + getInjectionSrc(injections);
4141
} else if (typeof injections === "string") {
4242
Module.wrapper[0] = nodeWrapper0 + injections;
4343
}

test/getInjectionSrc.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use strict"; // run code in ES5 strict mode
2+
3+
var expect = require("expect.js"),
4+
getInjectionSrc = require("../lib/getInjectionSrc.js");
5+
6+
describe("#getMonkeyPatchSrc", function () {
7+
it("should return ''", function () {
8+
var expectedSrc = "",
9+
subject = {};
10+
11+
expect(getInjectionSrc(subject)).to.be(expectedSrc);
12+
});
13+
it("should return 'var process={\"argv\": [\"myArg1\", \"myArg2\"]};var console=456;'", function () {
14+
var expectedSrc = "var process={\"argv\": [\"myArg1\", \"myArg2\"]};var console=456;",
15+
subject = {
16+
process: {
17+
argv: ["myArg1", "myArg2"]
18+
},
19+
console: 456
20+
};
21+
22+
expect(getInjectionSrc(subject)).to.be(expectedSrc);
23+
});
24+
});

test/getMonkeyPatchSrc.test.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

test/trick.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ describe("#trick", function () {
5757
};
5858

5959
tricked = trick("./testModules/A/moduleA.js", null, injections);
60-
expect(tricked.process).to.be(process); // the process object itself should not be changed
61-
expect(tricked.process.argv).to.be.eql(injections.process.argv);
60+
expect(tricked.process).not.to.be(process);
61+
expect(process.argv).not.to.eql(injections.process.argv);
62+
expect(tricked.process).to.eql(injections.process);
6263
expect(tricked.console).to.be(123);
6364
});
6465
it("should inject custom scripts", function () {

0 commit comments

Comments
 (0)