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

Add throwOnNotFound method #225

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,41 @@ Two simple steps to override require in your tests:
- therefore specify it exactly as in the require statement inside the tested file
- values themselves are key/value pairs of functions/properties and the appropriate override

## Throw if proxyquire tries to load a stub that does not exist

By default `proxyquire` will stub out modulePaths, whatever their path may be.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not line-wrap Markdown

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid "stub out," it's unclear what that means. It's also not true, as the sentence is written.

This includes if a dependency is not accessible (wrong file path) or not installed and accessible.

To raise an error if `proxyquire` attempts to load a module that does not exist,
you can utilize the *throwOnNotFound* function.

**Without throwOnNotFound:**
```javascript
proxyquire('./foo', {
'doesNotExist': () => {}
});
```

The above code snippet will not raise any errors.

**With throwOnNotFound:**
```javascript
var proxyquireThrowOnNotFound = require('proxyquire').throwOnNotFound();

proxyquireThrowOnNotFound('./foo', {
'doesNotExist': () => {}
});
```

This will throw an error when your tests run letting you know that proxyquire
cannot find `doesNotExist`. Typically, this is occurs when a file was renamed
in the calling module but was not updated in the proxyquire definition in the
test.

This method is also chainable with the *callThru* methods:

`var proxyquireThrowOnNotFoundAndNoCallThru = proxyquire.throwOnNotFound().noCallThru()`

## Preventing call thru to original dependency

By default proxyquire calls the function defined on the *original* dependency whenever it is not found on the stub.
Expand Down
17 changes: 17 additions & 0 deletions lib/proxyquire.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ Proxyquire.prototype.preserveCache = function () {
return this.fn
}

/**
* Stubbing a module that does not exist (from the perspective of the proxyquire
* subject) will throw an error.
* @name throwOnNotFound
* @function
* @private
* @return {object} The proxyquire function to allow chaining
*/
Proxyquire.prototype.throwOnNotFound = function () {
this._throwOnNotFound = true
return this.fn
}

/**
* Loads a module using the given stubs instead of their normally resolved required modules.
* @param request The requirable module path to load.
Expand Down Expand Up @@ -140,6 +153,10 @@ Proxyquire.prototype._resolveModule = function (baseModule, pathToResolve) {
paths: Module.globalPaths
})
} catch (err) {
if (this._throwOnNotFound) {
throw new ProxyquireError(err.message)
}

// If this is not a relative path (e.g. "foo" as opposed to "./foo"), and
// we couldn't resolve it, then we just let the path through unchanged.
// It's safe to do this, because if two different modules require "foo",
Expand Down
13 changes: 13 additions & 0 deletions test/proxyquire-argumentvalidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,17 @@ describe('Illegal parameters to resolve give meaningful errors', function () {
throws(act, /Invalid stub: "myStub" cannot be undefined/)
})
})

describe('when I pass a stub with a key that is not required on the proxyquired object', function () {
function act () {
proxyquire.throwOnNotFound()
proxyquire('./samples/foo', {
nonExistent: () => {}
})
}

it('throws an exception with the stub key', function () {
throws(act, /Cannot find module 'nonExistent'/)
})
})
})