Skip to content

Commit

Permalink
Move iterable check out of an() function
Browse files Browse the repository at this point in the history
  • Loading branch information
koddsson committed Feb 10, 2024
1 parent 0e2db37 commit f19bceb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
19 changes: 8 additions & 11 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,6 @@ function an (type, msg) {
, 'expected #{this} to be ' + article + type
, 'expected #{this} not to be ' + article + type
);
} else if (type === 'iterable') {
this.assert(
obj != undefined && obj[Symbol.iterator]
, 'expected #{this} to be ' + article + type
, 'expected #{this} not to be ' + article + type
);
} else {
this.assert(
type === detectedType
Expand Down Expand Up @@ -3197,11 +3191,14 @@ Assertion.addMethod('members', function (subset, msg) {
*/
Assertion.addProperty('iterable', function(msg) {
if (msg) flag(this, 'message', msg);
var obj = flag(this, 'object')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi');

new Assertion(obj, flagMsg, ssfi, true).to.be.an('iterable');
var obj = flag(this, 'object');

this.assert(
obj != undefined && obj[Symbol.iterator]
, 'expected #{this} to be an iterable'
, 'expected #{this} to not be an iterable'
, obj
);
});

/**
Expand Down
12 changes: 11 additions & 1 deletion lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -2498,7 +2498,17 @@ assert.oneOf = function (inList, list, msg) {
* @api public
*/
assert.isIterable = function(obj, msg) {
new Assertion(obj, msg, assert.isIterable, true).to.be.an('iterable');
if (obj == undefined || !obj[Symbol.iterator]) {
msg = msg ?
`${msg} expected ${inspect(obj)} to be an iterable` :
`expected ${inspect(obj)} to be an iterable`;

throw new AssertionError(
msg,
undefined,
assert.isIterable
);
}
}

/**
Expand Down

0 comments on commit f19bceb

Please sign in to comment.