Skip to content

Commit b474cee

Browse files
committed
feat(matchers): Allow for objects in oneOf
1 parent 179d470 commit b474cee

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

lib/chai/core/assertions.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,10 +1534,17 @@ module.exports = function (chai, _) {
15341534
/**
15351535
* ### .oneOf(list)
15361536
*
1537-
* Assert that a non-array, non-object value appears somewhere in the flat array `list`.
1537+
* Assert that a value appears somewhere in the top level of array `list`.
15381538
*
15391539
* expect('a').to.be.oneOf(['a', 'b', 'c']);
15401540
* expect(9).to.not.be.oneOf(['z']);
1541+
* expect([3]).to.not.be.oneOf([1, 2, [3]]);
1542+
*
1543+
* var three = [3];
1544+
* // for object-types, contents are not compared
1545+
* expect(three).to.not.be.oneOf([1, 2, [3]]);
1546+
* // comparing references works
1547+
* expect(three).to.be.oneOf([1, 2, three]);
15411548
*
15421549
* @name oneOf
15431550
* @param {Array<*>} list
@@ -1549,13 +1556,11 @@ module.exports = function (chai, _) {
15491556
if (msg) flag(this, 'message', msg);
15501557
var expected = flag(this, 'object');
15511558
new Assertion(list).to.be.an('array');
1552-
new Assertion(expected).to.not.be.an('array');
1553-
new Assertion(expected).to.not.be.an('object');
15541559

15551560
this.assert(
15561561
list.indexOf(expected) > -1
1557-
, 'expected #{this} to be in #{exp}'
1558-
, 'expected #{this} to not be in #{exp}'
1562+
, 'expected #{this} to be one of #{exp}'
1563+
, 'expected #{this} to not be one of #{exp}'
15591564
, list
15601565
, expected
15611566
);

test/assert.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -771,25 +771,31 @@ describe('assert', function () {
771771
it('oneOf', function() {
772772
assert.oneOf(1, [1, 2, 3]);
773773

774-
err(function() {
775-
assert.oneOf([1], []);
776-
}, 'expected [ 1 ] not to be an array');
774+
var three = [3];
775+
assert.oneOf(three, [1, 2, three]);
777776

778-
err(function() {
779-
assert.oneOf({a: 1}, []);
780-
}, 'expected { a: 1 } not to be an object');
777+
var four = { four: 4 };
778+
assert.oneOf(four, [1, 2, four]);
781779

782780
err(function() {
783781
assert.oneOf(1, 1);
784782
}, 'expected 1 to be an array');
785783

786784
err(function() {
787-
assert.oneOf(1, {a: 1});
785+
assert.oneOf(1, { a: 1 });
788786
}, 'expected { a: 1 } to be an array');
789787

790788
err(function() {
791789
assert.oneOf(9, [1, 2, 3], 'Message');
792-
}, 'Message: expected 9 to be in [ 1, 2, 3 ]');
790+
}, 'Message: expected 9 to be one of [ 1, 2, 3 ]');
791+
792+
err(function() {
793+
assert.oneOf([3], [1, 2, [3]]);
794+
}, 'expected [ 3 ] to be one of [ 1, 2, [ 3 ] ]');
795+
796+
err(function() {
797+
assert.oneOf({ four: 4 }, [1, 2, { four: 4 }]);
798+
}, 'expected { four: 4 } to be one of [ 1, 2, { four: 4 } ]');
793799

794800
});
795801

test/expect.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,9 @@ describe('expect', function () {
10821082
it('oneOf', function() {
10831083
expect(1).to.be.oneOf([1, 2, 3]);
10841084
expect('1').to.not.be.oneOf([1, 2, 3]);
1085+
expect([3, [4]]).to.not.be.oneOf([1, 2, [3, 4]]);
1086+
var threeFour = [3, [4]];
1087+
expect(threeFour).to.be.oneOf([1, 2, threeFour]);
10851088
});
10861089

10871090
it('include.members', function() {

0 commit comments

Comments
 (0)