Skip to content

Commit

Permalink
Should not blow up on modules that export primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
elicwhite committed Nov 30, 2015
1 parent a4fc3a8 commit b752ffb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/getDefinePropertySrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ var srcs = {
};

function getDefinePropertySrc() {
var src;
var src = "if (typeof(module.exports) === 'function' || \n" +
"(typeof(module.exports) === 'object' && module.exports !== null && Object.isExtensible(module.exports))) {\n";

src = Object.keys(srcs).reduce(function forEachSrc(preValue, value) {
src += Object.keys(srcs).reduce(function forEachSrc(preValue, value) {
return preValue += "Object.defineProperty(module.exports, '" +
value +
"', {enumerable: false, value: " +
Expand All @@ -22,6 +23,8 @@ function getDefinePropertySrc() {
"writable: true}); ";
}, "");

src += "\n}";

return src;
}

Expand Down
1 change: 1 addition & 0 deletions testLib/boolean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = true;
1 change: 1 addition & 0 deletions testLib/null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = null;
4 changes: 4 additions & 0 deletions testLib/sealedObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var obj = {};
Object.seal(obj);

module.exports = obj;
18 changes: 18 additions & 0 deletions testLib/sharedTestCases.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,24 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
expect(rewired.__get__("someVar")).to.be("hello");
});

it("should not be a problem to have a module that exports a boolean", function() {
expect(function() {
var rewired = rewire("./boolean.js");
}).to.not.throwException();
});

it("should not be a problem to have a module that exports null", function() {
expect(function() {
var rewired = rewire("./null.js");
}).to.not.throwException();
});

it("should not be a problem to have a module that exports a sealed object", function() {
expect(function() {
var rewired = rewire("./sealedObject.js");
}).to.not.throwException();
});

it("should not influence the original require if nothing has been required within the rewired module", function () {
rewire("./emptyModule.js"); // nothing happens here because emptyModule doesn't require anything
expect(require("./moduleA.js").__set__).to.be(undefined); // if restoring the original node require didn't worked, the module would have a setter
Expand Down

0 comments on commit b752ffb

Please sign in to comment.