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

Skip to content

Commit 4551fd8

Browse files
committed
Added fix for node 0.11.x tests
1 parent 5560d14 commit 4551fd8

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

lib/getImportGlobalsSrc.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ function getImportGlobalsSrc(ignore) {
1414
globalObj = typeof global === "undefined"? window: global;
1515

1616
ignore = ignore || [];
17+
// global itself can't be overridden because it's the only reference to our real global objects
18+
ignore.push("global");
1719

18-
for (key in globalObj) {
19-
if (key !== "global" && ignore.indexOf(key) === -1) { // we don't use hasOwnProperty here because in some browsers not all global objects will be enumerated
20-
value = globalObj[key];
21-
src += "var " + key + " = global." + key + "; ";
20+
for (key in globalObj) { /* jshint forin: false */
21+
if (ignore.indexOf(key) !== -1) {
22+
continue;
2223
}
24+
value = globalObj[key];
25+
src += "var " + key + " = global." + key + "; ";
2326
}
2427

25-
2628
return src;
2729
}
2830

test/getImportGlobalsSrc.test.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ describe("getImportGlobalsSrc", function () {
1313

1414
src = getImportGlobalsSrc();
1515
vm.runInNewContext(src, context);
16-
actualGlobals = Object.keys(context);
16+
actualGlobals = Object.keys(context).filter(function (key) {
17+
// node v0.10 does not set a constructor property on the context
18+
// node v0.11 does set a constructor property
19+
// so just lets filter it, because it doesn't make sense to mock it anyway
20+
return key !== "constructor";
21+
});
1722
actualGlobals.sort();
1823
expectedGlobals.sort();
1924
expect(actualGlobals).to.eql(expectedGlobals);
@@ -28,12 +33,18 @@ describe("getImportGlobalsSrc", function () {
2833
actualGlobals,
2934
expectedGlobals = Object.keys(global);
3035

31-
src = getImportGlobalsSrc(ignore);
36+
// getImportGlobalsSrc modifies the ignore array, so let's create a copy
37+
src = getImportGlobalsSrc(ignore.slice(0));
3238
expectedGlobals = expectedGlobals.filter(function filterIgnoredVars(value) {
3339
return ignore.indexOf(value) === -1;
3440
});
3541
vm.runInNewContext(src, context);
36-
actualGlobals = Object.keys(context);
42+
actualGlobals = Object.keys(context).filter(function (key) {
43+
// node v0.10 does not set a constructor property on the context
44+
// node v0.11 does set a constructor property
45+
// so just lets filter it, because it doesn't make sense to mock it anyway
46+
return key !== "constructor";
47+
});
3748
actualGlobals.sort();
3849
expectedGlobals.sort();
3950
expect(actualGlobals).to.eql(expectedGlobals);

0 commit comments

Comments
 (0)