Skip to content

Throw if performFn is valid but callback is not #13

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ function perform(action /* , args..., performFn, callback*/) {
if (typeof action !== 'string') throw new Error('event must be a string');
var callback = arguments[arguments.length - 1];
var performFn = arguments[arguments.length - 2];
if (typeof callback !== 'function') {
throw new Error('last argument must be a function');
}

// Allow callback to be omitted by using last arg as performFn
var slice = -2;
if (typeof performFn !== 'function') {
if (typeof callback !== 'function') {
throw new Error('performFn and callback must be a function');
}

performFn = callback;
callback = null;
slice = -1;
Expand Down
15 changes: 11 additions & 4 deletions test/no-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ var Understudy = require('../').Understudy;
var actor = new Understudy();

actor.before('no-functions', function () {
assert.equal(true, false);
throw new Error('before shouldn\'t be reached');
});

actor.after('no-functions', function () {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe you also want to make the same change you made to the actor.before statement above.

Copy link
Author

Choose a reason for hiding this comment

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

If you look at the file after changes, I deleted actor.after and modified actor.before. I think actor.after is redundant in this case.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not redundant. It's an additional assertion that should never be hit: after hooks should not be executed. If they are it's a bug.

assert.equal(true, false);
throw new Error('after shouldn\'t be reached');
});

assert.throws(function () {
actor.perform('no-functions', 'NO', 'AFTER');
});
actor.perform('no-functions', 'INVALID ACTION', 'INVALID CALLBACK');
}, /last argument must be a function/);

assert.throws(function () {
function action () {
throw new Error('action shouldn\'t be reached');
}
actor.perform('no-functions', action, 'INVALID CALLBACK');
}, /last argument must be a function/);