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
TheSavior committed Nov 7, 2015
1 parent ac0c8a3 commit a23e60e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/getDefinePropertySrc.js
Expand Up @@ -11,9 +11,10 @@ var srcs = {
};

function getDefinePropertySrc() {
var src;
var src = "if (typeof(module.exports) === 'function' || \n" +
"typeof(module.exports) === 'object') {\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 test/testModules/boolean.js
@@ -0,0 +1 @@
module.exports = true;
1 change: 1 addition & 0 deletions test/testModules/sealedObject.js
@@ -0,0 +1 @@
module.exports = {};
12 changes: 12 additions & 0 deletions test/testModules/sharedTestCases.js
Expand Up @@ -220,6 +220,18 @@ 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 primitive", function() {
expect(function() {
var rewired = rewire("./boolean.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 a23e60e

Please sign in to comment.