From 4a8faf02cc7faf5b5735210421597c0203d0ec41 Mon Sep 17 00:00:00 2001 From: Harry Brundage Date: Fri, 25 Jun 2021 11:07:45 -0400 Subject: [PATCH] Add an option to intercept and rewire nested requires Follow up to #73 --- lib/index.js | 4 ++-- lib/rewire.js | 18 +++++++++++++++++- testLib/sharedTestCases.js | 13 +++++++++++++ testLib/someOtherModule.js | 1 + 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 0edcd2c..d8b89c3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -7,8 +7,8 @@ var rewireModule = require("./rewire.js"); * @param {!String} filename Path to the module that shall be rewired. Use it exactly like require(). * @return {*} the rewired module */ -function rewire(filename) { - return rewireModule(module.parent, filename); +function rewire(filename, rewireNestedRequires) { + return rewireModule(module.parent, filename, rewireNestedRequires); } module.exports = rewire; diff --git a/lib/rewire.js b/lib/rewire.js index 3ac2042..bdc01af 100644 --- a/lib/rewire.js +++ b/lib/rewire.js @@ -8,7 +8,7 @@ var Module = require("module"), /** * Does actual rewiring the module. For further documentation @see index.js */ -function internalRewire(parentModulePath, targetPath) { +function internalRewire(parentModulePath, targetPath, rewireNestedRequires) { var targetModule, prelude, appendix, @@ -28,6 +28,22 @@ function internalRewire(parentModulePath, targetPath) { // We prepend a list of all globals declared with var so they can be overridden (without changing original globals) prelude = getImportGlobalsSrc(); + // If asked, intercept require calls when evaluating the rewired module to rewire those inner requires + if (rewireNestedRequires) { + targetModule.__rewire__ = internalRewire; + + prelude += ` + const __oldRequire = require; + var require = function(path) { + if (module.__rewire__) { + return module.__rewire__(module.parent, path); + } else { + return __oldRequire(path); + } + }; + ` + } + // Wrap module src inside IIFE so that function declarations do not clash with global variables // @see https://github.com/jhnns/rewire/issues/56 prelude += "(function () { "; diff --git a/testLib/sharedTestCases.js b/testLib/sharedTestCases.js index a4158d9..f4c5372 100644 --- a/testLib/sharedTestCases.js +++ b/testLib/sharedTestCases.js @@ -408,4 +408,17 @@ module.exports = function () { }).to.throwException(/^Assignment to constant variable at .+?wrongConstModule\.js:4:1$/); }); + it("should not rewire nested requires by default", function () { + const a = rewire('./moduleA') + const b = rewire('./moduleB') + + expect(a.someOtherModule.date).to.equal(b.someOtherModule.date) + }) + + it("should rewire nested requires by default", function () { + const a = rewire('./moduleA', true) + const b = rewire('./moduleB', true) + + expect(a.someOtherModule.date).not.to.equal(b.someOtherModule.date) + }) }; diff --git a/testLib/someOtherModule.js b/testLib/someOtherModule.js index fe6bbd7..f17e1e9 100644 --- a/testLib/someOtherModule.js +++ b/testLib/someOtherModule.js @@ -5,3 +5,4 @@ __filename = "/test/testModules/someOtherModule.js"; exports.fs = {}; exports.filename = __filename; exports.name = "somOtherModule"; +exports.date = new Date();