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 a descriptive error when a non-function is given to a runnable #4133

Merged
merged 1 commit into from Jan 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/runnable.js
Expand Up @@ -338,6 +338,15 @@ Runnable.prototype.run = function(fn) {
// for .resetTimeout()
this.callback = done;

if (this.fn && typeof this.fn.call !== 'function') {
done(
new TypeError(
'A runnable must be passed a function as its second argument.'
)
);
return;
}

// explicit async with `done` argument
if (this.async) {
this.resetTimeout();
Expand Down
14 changes: 14 additions & 0 deletions test/unit/runnable.spec.js
Expand Up @@ -670,6 +670,20 @@ describe('Runnable(title, fn)', function() {
});
});
});

describe('when fn is not a function', function() {
it('should throw an error', function() {
var runnable = new Runnable('foo', 4);

runnable.run(function(err) {
expect(
err.message,
'to be',
'A runnable must be passed a function as its second argument.'
);
});
});
});
});

describe('#isFailed()', function() {
Expand Down