Skip to content

Commit

Permalink
introduce isAsyncFunction and isNotAsyncFunction assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson committed Dec 29, 2023
1 parent 9d8c5bf commit 2f513d8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
36 changes: 36 additions & 0 deletions lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,42 @@ assert.isNotFunction = function (val, msg) {
new Assertion(val, msg, assert.isNotFunction, true).to.not.be.a('function');
};

/**
* ### .isAsyncFunction(value, [message])
*
* Asserts that `value` is a async function.
*
* async function serveTea() { return 'cup of tea'; };
* assert.isAsyncFunction(serveTea, 'great, we can have tea now');
*
* @name isAsyncFunction
* @param {Mixed} value
* @param {String} message
* @namespace Assert
* @api public
*/
assert.isAsyncFunction = function (val, msg) {
new Assertion(val, msg, assert.isAsyncFunction, true).to.be.a('asyncfunction');
};

/**
* ### .isNotAsyncFunction(value, [message])
*
* Asserts that `value` is _not_ a async function.
*
* var serveTea = [ 'heat', 'pour', 'sip' ];
* assert.isNotAsyncFunction(serveTea, 'great, we have listed the steps');
*
* @name isNotAsyncFunction
* @param {Mixed} value
* @param {String} message
* @namespace Assert
* @api public
*/
assert.isNotAsyncFunction = function (val, msg) {
new Assertion(val, msg, assert.isNotAsyncFunction, true).to.not.be.a('asyncfunction');
};

/**
* ### .isObject(value, [message])
*
Expand Down
20 changes: 17 additions & 3 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,6 @@ describe('assert', function () {
it('isFunction', function() {
var func = function() {};
assert.isFunction(func);

async function asyncFunction() {};
assert.isFunction(asyncFunction);

err(function () {
assert.isFunction({}, 'blah');
Expand All @@ -540,6 +537,23 @@ describe('assert', function () {
assert.isNotFunction(function () {}, 'blah');
}, "blah: expected [Function] not to be a function");
});

it('isAsyncFunction', function() {
async function func() {};
assert.isAsyncFunction(func);

err(function () {
assert.isAsyncFunction(function () {}, 'blah');
}, "blah: expected [Function] to be an asyncfunction");
});

it('isNotAsyncFunction', function () {
assert.isNotAsyncFunction(5);

err(function () {
assert.isNotAsyncFunction(async function () {}, 'blah');
}, "blah: expected [Function] not to be an asyncfunction");
});

it('isArray', function() {
assert.isArray([]);
Expand Down

0 comments on commit 2f513d8

Please sign in to comment.