proxyquire
Advanced tools
Comparing version 2.1.1 to 2.1.2
@@ -135,3 +135,3 @@ 'use strict' | ||
// module we want to resolve (i.e. the string passed to `require()`). | ||
Proxyquire.prototype._resolveModule = function (baseModule, pathToResolve) { | ||
Proxyquire.prototype._resolveModule = function (baseModule, pathToResolve, stubs) { | ||
try { | ||
@@ -157,6 +157,19 @@ return resolve.sync(pathToResolve, { | ||
// throw, since we can resolve modules that don't exist. Resolve as | ||
// best we can. | ||
if (!this._preserveCache || this._noCallThru) { | ||
return path.resolve(path.dirname(baseModule), pathToResolve) | ||
// best we can. We also need to check if the relative module has @noCallThru. | ||
var moduleNoCallThru | ||
var resolvedPath | ||
if (hasOwnProperty.call(stubs, pathToResolve)) { | ||
// pathToResolve is currently relative on stubs from _withoutCache() call | ||
moduleNoCallThru = hasOwnProperty.call(stubs[pathToResolve], '@noCallThru') ? stubs[pathToResolve]['@noCallThru'] : undefined | ||
resolvedPath = path.resolve(path.dirname(baseModule), pathToResolve) | ||
} else { | ||
resolvedPath = path.resolve(path.dirname(baseModule), pathToResolve) | ||
if (hasOwnProperty.call(stubs, resolvedPath)) { | ||
// after _withoutCache() alters stubs paths to be absolute | ||
moduleNoCallThru = hasOwnProperty.call(stubs[resolvedPath], '@noCallThru') ? stubs[resolvedPath]['@noCallThru'] : undefined | ||
} | ||
} | ||
if (!this._preserveCache || this._noCallThru || moduleNoCallThru) { | ||
return resolvedPath | ||
} | ||
@@ -172,6 +185,5 @@ throw err | ||
var resolvedPath = this._resolveModule(module.filename, path) | ||
var resolvedPath = this._resolveModule(module.filename, path, stubs) | ||
if (hasOwnProperty.call(stubs, resolvedPath)) { | ||
var stub = stubs[resolvedPath] | ||
if (stub === null) { | ||
@@ -208,7 +220,6 @@ // Mimic the module-not-found exception thrown by node.js. | ||
.reduce(function (result, stubPath) { | ||
var resolvedStubPath = this._resolveModule(resolvedPath, stubPath) | ||
var resolvedStubPath = this._resolveModule(resolvedPath, stubPath, stubs) | ||
result[resolvedStubPath] = stubs[stubPath] | ||
return result | ||
}.bind(this), {}) | ||
// Override all require extension handlers | ||
@@ -294,3 +305,2 @@ var restoreExtensionHandlers = this._overrideExtensionHandlers(module, stubs) | ||
/* eslint node/no-deprecated-api: [error, {ignoreGlobalItems: ["require.extensions"]}] */ | ||
var originalExtensions = {} | ||
@@ -297,0 +307,0 @@ var self = this |
{ | ||
"name": "proxyquire", | ||
"version": "2.1.1", | ||
"version": "2.1.2", | ||
"description": "Proxies nodejs require in order to allow overriding dependencies during testing.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,2 +7,3 @@ 'use strict' | ||
var fooPath = path.join(__dirname, './samples/notexisting/foo.js') | ||
var fooRelativePath = path.join(__dirname, './samples/notexisting/foo-relative.js') | ||
@@ -38,1 +39,10 @@ describe('When resolving foo that requires stubbed /not/existing/bar.json', function () { | ||
}) | ||
describe('When resolving foo-relative that requires relative stubbed ../not/existing/bar.json with @noCallThru', function () { | ||
it('resolves foo-relative with stubbed bar', function () { | ||
var fooRelative = proxyquire(fooRelativePath, { | ||
'../not/existing/bar.json': { config: 'bar\'s config', '@noCallThru': true } | ||
}) | ||
assert.strictEqual(fooRelative.config, 'bar\'s config') | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67168
62
1413