Skip to content

Commit

Permalink
fix: throw error if no function passed to pre/post
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Aug 22, 2019
1 parent 71e24f4 commit c0dd81c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ Kareem.prototype.pre = function(name, isAsync, fn, error, unshift) {
++pres.numAsync;
}

if (typeof fn !== 'function') {
throw new Error('pre() requires a function, got "' + typeof fn + '"');
}

if (unshift) {
pres.unshift(Object.assign({}, options, { fn: fn, isAsync: isAsync }));
} else {
Expand All @@ -413,6 +417,10 @@ Kareem.prototype.post = function(name, options, fn, unshift) {
options = {};
}

if (typeof fn !== 'function') {
throw new Error('post() requires a function, got "' + typeof fn + '"');
}

if (unshift) {
hooks.unshift(Object.assign({}, options, { fn: fn }));
} else {
Expand Down
4 changes: 4 additions & 0 deletions test/post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ describe('execPost', function() {
assert.equal(hooks._posts.get('cook')[0].bar, 'baz');
});

it('throws error if no function', function() {
assert.throws(() => hooks.post('test'), /got "undefined"/);
});

it('multiple posts', function(done) {
hooks.post('cook', function(eggs, callback) {
setTimeout(
Expand Down
4 changes: 4 additions & 0 deletions test/pre.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ describe('execPre', function() {
assert.strictEqual(hooks._pres.get('cook')[1].fn, f1);
});

it('throws error if no function', function() {
assert.throws(() => hooks.pre('test'), /got "undefined"/);
});

it('arbitrary options', function() {
const f1 = function() {};
const f2 = function() {};
Expand Down

0 comments on commit c0dd81c

Please sign in to comment.