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

Throw an informative error on missing modules. #68

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ module.exports = function(options) {
// This searches up from the specified package.json file, making sure
// the config option behaves as expected. See issue #56.
var searchFor = path.join('node_modules', name);
return require(findup(searchFor, {cwd: path.dirname(config)}));
var src = findup(searchFor, {cwd: path.dirname(config)});
if (src !== null) {
return require(src);
} else {
throw new Error('Cannot find `' + name + '` in your node_modules!');
}
};
} else {
requireFn = require;
Expand Down
10 changes: 9 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,15 @@ describe('with lazy loading', function() {
});
});

describe('requiring from another directory', function() {
describe('common functionality', function () {
it('throws a sensible error when not found', function () {
var x = gulpLoadPlugins({ config: __dirname + '/package.json' });
Copy link
Owner

Choose a reason for hiding this comment

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

can you just pass in a config as an object config: {} and then assert that x.notThere() throws, or similar, rather than loading in the package.json? Keeps the test config all in the specs.

I do need to tidy up the specs though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The error only happens when we're passed a path (string) or undefined as the config, (L23 and 32). Passing in an object would cause the default require to be used (on L45), which already throws a helpful error.

Copy link
Owner

Choose a reason for hiding this comment

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

👍


assert.throws(function () {
x.oops();
}, (/Cannot find `gulp-oops`/));
});

it('allows you to use in a lower directory', function() {
var plugins = require('../')();
assert.ok(typeof plugins.test === 'function');
Expand Down
3 changes: 2 additions & 1 deletion test/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"dependencies": {
"gulp-test": "*"
"gulp-test": "*",
"gulp-oops": "*"
}
}