Skip to content

Commit

Permalink
Fix: Bail gracefully on unknown extensions when nothrow is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Kellen authored and phated committed Jan 4, 2019
1 parent 77cbc95 commit 529d9c0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ exports.prepare = function (extensions, filepath, cwd, nothrow) {
}
var config = normalize(extensions[ext]);
if (!config) {
throw new Error('No module loader found for "'+ext+'".');
if (nothrow) {
return;
} else {
throw new Error('No module loader found for "'+ext+'".');
}
}
if (!cwd) {
cwd = path.dirname(path.resolve(filepath));
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/.testrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
data: {
trueKey: true,
falseKey: false,
subKey: {
subProp: 1
}
}
};
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ describe('rechoir', function () {
}).to.throw(/No module loader found for/);
});

it('should return undefined if an unknown extension is specified when nothrow is enabled', function () {
expect(rechoir.prepare(extensions, './test/fixtures/.testrc', null, true)).to.be.undefined;
});

it('should throw if a module loader cannot be found or loaded', function () {
expect(function () {
require(testFilePath);
Expand Down

0 comments on commit 529d9c0

Please sign in to comment.