Skip to content

Commit

Permalink
Throw when a stub is undefined to help prevent user error
Browse files Browse the repository at this point in the history
  • Loading branch information
bendrucker committed Aug 17, 2015
1 parent d96e4d8 commit e8ee64c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/proxyquire.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,19 @@ Proxyquire.prototype.load = function (request, stubs) {

// Find out if any of the passed stubs are global overrides
for (var key in stubs) {
if (stubs[key] === null) continue;
var stub = stubs[key];

if (hasOwnProperty.call(stubs[key], '@global')) {
if (stub === null) continue;

if (typeof stub === 'undefined') {
throw new ProxyquireError('Invalid stub: "' + key + '" cannot be undefined');
}

if (hasOwnProperty.call(stub, '@global')) {
this._containsGlobal = true;
}

if (hasOwnProperty.call(stubs[key], '@runtimeGlobal')) {
if (hasOwnProperty.call(stub, '@runtimeGlobal')) {
this._containsGlobal = true;
this._containsRuntimeGlobal = true;
}
Expand Down
12 changes: 12 additions & 0 deletions test/proxyquire-argumentvalidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ describe('Illegal parameters to resolve give meaningful errors', function () {
throws(act, /invalid argument: "stubs".+needs to be an object/i);
})
})

describe('when I pass an undefined stub', function () {
function act () {
proxyquire('./samples/foo', {
myStub: undefined
})
}

it('throws an exception with the stub key', function () {
throws(act, /Invalid stub: "myStub" cannot be undefined/)
})
})
})

0 comments on commit e8ee64c

Please sign in to comment.