Skip to content

Commit

Permalink
Merge pull request #83 from TheSavior/primitives
Browse files Browse the repository at this point in the history
Should not try to attach to modules that export primitives
  • Loading branch information
jhnns committed Nov 30, 2015
2 parents a4fc3a8 + b752ffb commit e124971
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
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
@@ -0,0 +1 @@
module.exports = true;
1 change: 1 addition & 0 deletions testLib/null.js
@@ -0,0 +1 @@
module.exports = null;
4 changes: 4 additions & 0 deletions testLib/sealedObject.js
@@ -0,0 +1,4 @@
var obj = {};
Object.seal(obj);

module.exports = obj;
18 changes: 18 additions & 0 deletions testLib/sharedTestCases.js
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 e124971

Please sign in to comment.