Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e124971

Browse files
committed
Merge pull request jhnns#83 from TheSavior/primitives
Should not try to attach to modules that export primitives
2 parents a4fc3a8 + b752ffb commit e124971

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

lib/getDefinePropertySrc.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ var srcs = {
1111
};
1212

1313
function getDefinePropertySrc() {
14-
var src;
14+
var src = "if (typeof(module.exports) === 'function' || \n" +
15+
"(typeof(module.exports) === 'object' && module.exports !== null && Object.isExtensible(module.exports))) {\n";
1516

16-
src = Object.keys(srcs).reduce(function forEachSrc(preValue, value) {
17+
src += Object.keys(srcs).reduce(function forEachSrc(preValue, value) {
1718
return preValue += "Object.defineProperty(module.exports, '" +
1819
value +
1920
"', {enumerable: false, value: " +
@@ -22,6 +23,8 @@ function getDefinePropertySrc() {
2223
"writable: true}); ";
2324
}, "");
2425

26+
src += "\n}";
27+
2528
return src;
2629
}
2730

testLib/boolean.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = true;

testLib/null.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = null;

testLib/sealedObject.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var obj = {};
2+
Object.seal(obj);
3+
4+
module.exports = obj;

testLib/sharedTestCases.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,24 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
220220
expect(rewired.__get__("someVar")).to.be("hello");
221221
});
222222

223+
it("should not be a problem to have a module that exports a boolean", function() {
224+
expect(function() {
225+
var rewired = rewire("./boolean.js");
226+
}).to.not.throwException();
227+
});
228+
229+
it("should not be a problem to have a module that exports null", function() {
230+
expect(function() {
231+
var rewired = rewire("./null.js");
232+
}).to.not.throwException();
233+
});
234+
235+
it("should not be a problem to have a module that exports a sealed object", function() {
236+
expect(function() {
237+
var rewired = rewire("./sealedObject.js");
238+
}).to.not.throwException();
239+
});
240+
223241
it("should not influence the original require if nothing has been required within the rewired module", function () {
224242
rewire("./emptyModule.js"); // nothing happens here because emptyModule doesn't require anything
225243
expect(require("./moduleA.js").__set__).to.be(undefined); // if restoring the original node require didn't worked, the module would have a setter

0 commit comments

Comments
 (0)