Skip to content

Commit 817284c

Browse files
feat: add "deep" flag in oneOf (#1334)
1 parent 6c963d0 commit 817284c

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

lib/chai/core/assertions.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,7 +3150,8 @@ module.exports = function (chai, _) {
31503150
var expected = flag(this, 'object')
31513151
, flagMsg = flag(this, 'message')
31523152
, ssfi = flag(this, 'ssfi')
3153-
, contains = flag(this, 'contains');
3153+
, contains = flag(this, 'contains')
3154+
, isDeep = flag(this, 'deep');
31543155
new Assertion(list, flagMsg, ssfi, true).to.be.an('array');
31553156

31563157
if (contains) {
@@ -3162,13 +3163,23 @@ module.exports = function (chai, _) {
31623163
, expected
31633164
);
31643165
} else {
3165-
this.assert(
3166-
list.indexOf(expected) > -1
3167-
, 'expected #{this} to be one of #{exp}'
3168-
, 'expected #{this} to not be one of #{exp}'
3169-
, list
3170-
, expected
3171-
);
3166+
if (isDeep) {
3167+
this.assert(
3168+
list.some(possibility => _.eql(expected, possibility))
3169+
, 'expected #{this} to deeply equal one of #{exp}'
3170+
, 'expected #{this} to deeply equal one of #{exp}'
3171+
, list
3172+
, expected
3173+
);
3174+
} else {
3175+
this.assert(
3176+
list.indexOf(expected) > -1
3177+
, 'expected #{this} to be one of #{exp}'
3178+
, 'expected #{this} to not be one of #{exp}'
3179+
, list
3180+
, expected
3181+
);
3182+
}
31723183
}
31733184
}
31743185

test/expect.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3308,6 +3308,7 @@ describe('expect', function () {
33083308
expect([3, [4]]).to.not.be.oneOf([1, 2, [3, 4]]);
33093309
var threeFour = [3, [4]];
33103310
expect(threeFour).to.be.oneOf([1, 2, threeFour]);
3311+
expect([]).to.be.deep.oneOf([[], '']);
33113312

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

0 commit comments

Comments
 (0)