From 562f8a6343b9ff2bb667f687a9e799f11d424d9a Mon Sep 17 00:00:00 2001 From: Eli White Date: Sat, 7 Nov 2015 13:52:31 -0800 Subject: [PATCH] Should not blow up on modules that export primitives --- lib/getDefinePropertySrc.js | 7 +++++-- testLib/boolean.js | 1 + testLib/sealedObject.js | 4 ++++ testLib/sharedTestCases.js | 12 ++++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 testLib/boolean.js create mode 100644 testLib/sealedObject.js diff --git a/lib/getDefinePropertySrc.js b/lib/getDefinePropertySrc.js index b856a7f..039f442 100644 --- a/lib/getDefinePropertySrc.js +++ b/lib/getDefinePropertySrc.js @@ -11,9 +11,10 @@ var srcs = { }; function getDefinePropertySrc() { - var src; + var src = "if (typeof(module.exports) === 'function' || \n" + + "(typeof(module.exports) === 'object' && 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: " + @@ -22,6 +23,8 @@ function getDefinePropertySrc() { "writable: true}); "; }, ""); + src += "\n}"; + return src; } diff --git a/testLib/boolean.js b/testLib/boolean.js new file mode 100644 index 0000000..ec01c2c --- /dev/null +++ b/testLib/boolean.js @@ -0,0 +1 @@ +module.exports = true; diff --git a/testLib/sealedObject.js b/testLib/sealedObject.js new file mode 100644 index 0000000..72881a8 --- /dev/null +++ b/testLib/sealedObject.js @@ -0,0 +1,4 @@ +var obj = {}; +Object.seal(obj); + +module.exports = obj; diff --git a/testLib/sharedTestCases.js b/testLib/sharedTestCases.js index 9fb83ed..df9a06a 100644 --- a/testLib/sharedTestCases.js +++ b/testLib/sharedTestCases.js @@ -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