Equivalent to rspec = ~ for arrays in Chai

Does Chai have a consistent rspecs =~ (which means all the elements, but the order doesn't matter.

Transition Example

 [1, 2, 3].should =~ [2, 1, 3] 

Otherwise

 [1, 2, 3].should =~ [1, 2] 
+6
source share
3 answers

You can use the members test, which is available in the latest version of Chai:

 expect([4, 2]).to.have.members([2, 4]); expect([5, 2]).to.not.have.members([5, 2, 1]); 
+10
source

I don’t think there is, but you can easily create it by creating an assistant :

 var chai = require('chai'), expect = chai.expect, assert = chai.assert, Assertion = chai.Assertion Assertion.addMethod('equalAsSets', function (otherArray) { var array = this._obj; expect(array).to.be.an.instanceOf(Array); expect(otherArray).to.be.an.instanceOf(Array); var diff = array.filter(function(i) {return !(otherArray.indexOf(i) > -1);}); this.assert( diff.length === 0, "expected #{this} to be equal to #{exp} (as sets, ie no order)", array, otherArray ); }); expect([1,2,3]).to.be.equalAsSets([1,3,2]); expect([1,2,3]).to.be.equalAsSets([3,2]); flag 

Remember that this is not an unordered equality test; it establishes equality. Duplicate elements are allowed in any array; this passes, for example: expect([1,2,3]).to.be.equalAsSets([1,3,2,2]);

+5
source

Source: https://habr.com/ru/post/920593/


All Articles