From ab1e13a0951db3909a15b14e588b3d8a4f11fd05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Oddsson?= Date: Mon, 15 Jan 2024 18:09:50 +0100 Subject: [PATCH] Add negation to other callable assertions --- lib/chai/core/assertions.js | 49 ++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index b6376d43..2a71d775 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -709,14 +709,25 @@ Assertion.addProperty('callable', function () { } }); +/** + * @todo + */ Assertion.addProperty('asyncFunction', function () { const val = flag(this, 'object') const ssfi = flag(this, 'ssfi') - const msg = `${flag(this, 'message')}: ` || '' + const message = flag(this, 'message') + const msg = message ? `${message}: ` : '' + const negate = flag(this, 'negate'); + + const assertionMessage = negate ? + `${msg}expected ${_.inspect(val)} not to be a AsyncFunction` : + `${msg}expected ${_.inspect(val)} to be a AsyncFunction`; + + const isAsyncCallable = ['AsyncFunction', 'AsyncGeneratorFunction'].includes(_.type(val)); - if (!['AsyncFunction', 'AsyncGeneratorFunction'].includes(_.type(val))) { + if ((isAsyncCallable && negate) || (!isAsyncCallable && !negate)) { throw new AssertionError( - msg + 'expected ' + _.inspect(val) + ' to be a AsyncFunction', + assertionMessage, undefined, ssfi ); @@ -724,16 +735,24 @@ Assertion.addProperty('asyncFunction', function () { }); /** - * TODO + * @todo */ Assertion.addProperty('generatorFunction', function() { const val = flag(this, 'object') const ssfi = flag(this, 'ssfi') - const msg = `${flag(this, 'message')}: ` || '' + const message = flag(this, 'message') + const msg = message ? `${message}: ` : '' + const negate = flag(this, 'negate'); - if (!['GeneratorFunction', 'AsyncGeneratorFunction'].includes(_.type(val))) { + const assertionMessage = negate ? + `${msg}expected ${_.inspect(val)} not to be a GeneratorFunction` : + `${msg}expected ${_.inspect(val)} to be a GeneratorFunction`; + + const isGeneratorCallable = ['GeneratorFunction', 'AsyncGeneratorFunction'].includes(_.type(val)); + + if ((isGeneratorCallable && negate) || (!isGeneratorCallable && !negate)) { throw new AssertionError( - msg + 'expected ' + _.inspect(val) + ' to be a GeneratorFunction', + assertionMessage, undefined, ssfi ); @@ -741,16 +760,24 @@ Assertion.addProperty('generatorFunction', function() { }); /** - * TODO + * @todo */ Assertion.addProperty('asyncGeneratorFunction', function() { const val = flag(this, 'object') const ssfi = flag(this, 'ssfi') - const msg = `${flag(this, 'message')}: ` || '' + const message = flag(this, 'message') + const msg = message ? `${message}: ` : '' + const negate = flag(this, 'negate'); + + const assertionMessage = negate ? + `${msg}expected ${_.inspect(val)} not to be a AsyncGeneratorFunction` : + `${msg}expected ${_.inspect(val)} to be a AsyncGeneratorFunction`; - if (_.type(val) !== 'AsyncGeneratorFunction') { + const isAsyncGeneratorCallable = _.type(val) === 'AsyncGeneratorFunction'; + + if ((isAsyncGeneratorCallable && negate) || (!isAsyncGeneratorCallable && !negate)) { throw new AssertionError( - msg + 'expected ' + _.inspect(val) + ' to be a AsyncGeneratorFunction', + assertionMessage, undefined, ssfi );