From a23e60ed1ddc55750728ebc35ab27e10f9f23688 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 +++++-- test/testModules/boolean.js | 1 + test/testModules/sealedObject.js | 1 + test/testModules/sharedTestCases.js | 12 ++++++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 test/testModules/boolean.js create mode 100644 test/testModules/sealedObject.js diff --git a/lib/getDefinePropertySrc.js b/lib/getDefinePropertySrc.js index b856a7f..71964be 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') {\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/test/testModules/boolean.js b/test/testModules/boolean.js new file mode 100644 index 0000000..ec01c2c --- /dev/null +++ b/test/testModules/boolean.js @@ -0,0 +1 @@ +module.exports = true; diff --git a/test/testModules/sealedObject.js b/test/testModules/sealedObject.js new file mode 100644 index 0000000..f053ebf --- /dev/null +++ b/test/testModules/sealedObject.js @@ -0,0 +1 @@ +module.exports = {}; diff --git a/test/testModules/sharedTestCases.js b/test/testModules/sharedTestCases.js index ccbafa3..242f881 100644 --- a/test/testModules/sharedTestCases.js +++ b/test/testModules/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