Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should not try to attach to modules that export primitives #83

Merged
merged 1 commit into from Nov 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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