From 7132c125d6e8ec9b3525b0c1a24faaa3e8a7f209 Mon Sep 17 00:00:00 2001 From: nehardt Date: Mon, 14 Sep 2015 11:10:11 -0700 Subject: [PATCH 1/2] Throw if performFn is valid but callback is not --- index.js | 9 +++++---- test/no-functions.js | 12 ++++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index dfaa0fe..2a41cd8 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/test/no-functions.js b/test/no-functions.js index f319fdd..b29358a 100644 --- a/test/no-functions.js +++ b/test/no-functions.js @@ -5,13 +5,17 @@ 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 () { - assert.equal(true, false); +assert.throws(function () { + actor.perform('no-functions', 'INVALID ACTION', 'INVALID CALLBACK'); +}, function (err) { + return err.message == 'last argument must be a function'; }); assert.throws(function () { - actor.perform('no-functions', 'NO', 'AFTER'); + actor.perform('no-functions', function () {}, 'INVALID CALLBACK'); +}, function (err) { + return err.message == 'last argument must be a function'; }); From 0e57e1db39102e6b815b44f434501a3f1197be84 Mon Sep 17 00:00:00 2001 From: nehardt Date: Mon, 14 Sep 2015 14:05:23 -0700 Subject: [PATCH 2/2] Add explicit errors for `after` and `action` --- test/no-functions.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/test/no-functions.js b/test/no-functions.js index b29358a..84eca7d 100644 --- a/test/no-functions.js +++ b/test/no-functions.js @@ -8,14 +8,17 @@ actor.before('no-functions', function () { throw new Error('before shouldn\'t be reached'); }); +actor.after('no-functions', function () { + throw new Error('after shouldn\'t be reached'); +}); + assert.throws(function () { actor.perform('no-functions', 'INVALID ACTION', 'INVALID CALLBACK'); -}, function (err) { - return err.message == 'last argument must be a function'; -}); +}, /last argument must be a function/); assert.throws(function () { - actor.perform('no-functions', function () {}, 'INVALID CALLBACK'); -}, function (err) { - return err.message == 'last argument must be a function'; -}); + function action () { + throw new Error('action shouldn\'t be reached'); + } + actor.perform('no-functions', action, 'INVALID CALLBACK'); +}, /last argument must be a function/);