Skip to content

Commit

Permalink
feat: add "deep" flag in oneOf (#1334)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgroothuijsen authored Feb 4, 2021
1 parent 6c963d0 commit 817284c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3150,7 +3150,8 @@ module.exports = function (chai, _) {
var expected = flag(this, 'object')
, flagMsg = flag(this, 'message')
, ssfi = flag(this, 'ssfi')
, contains = flag(this, 'contains');
, contains = flag(this, 'contains')
, isDeep = flag(this, 'deep');
new Assertion(list, flagMsg, ssfi, true).to.be.an('array');

if (contains) {
Expand All @@ -3162,13 +3163,23 @@ module.exports = function (chai, _) {
, expected
);
} else {
this.assert(
list.indexOf(expected) > -1
, 'expected #{this} to be one of #{exp}'
, 'expected #{this} to not be one of #{exp}'
, list
, expected
);
if (isDeep) {
this.assert(
list.some(possibility => _.eql(expected, possibility))
, 'expected #{this} to deeply equal one of #{exp}'
, 'expected #{this} to deeply equal one of #{exp}'
, list
, expected
);
} else {
this.assert(
list.indexOf(expected) > -1
, 'expected #{this} to be one of #{exp}'
, 'expected #{this} to not be one of #{exp}'
, list
, expected
);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3308,6 +3308,7 @@ describe('expect', function () {
expect([3, [4]]).to.not.be.oneOf([1, 2, [3, 4]]);
var threeFour = [3, [4]];
expect(threeFour).to.be.oneOf([1, 2, threeFour]);
expect([]).to.be.deep.oneOf([[], '']);

expect([1, 2]).to.contain.oneOf([4,2,5]);
expect([3, 4]).to.not.contain.oneOf([2,1,5]);
Expand Down

0 comments on commit 817284c

Please sign in to comment.