0

Let's say in response during tests I have such array:

array = [
 'a',   'b',
 'c',   'd',
 'e',   'f',
 'g',   'h',
 'i',   'j'
]

And I want to check if this array really contains those values:

array.should.be.a.Array()
.with.lengthOf(response.body.length)
.and.have.properties('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');

As you can see I can't use here have.properties because it's array, not object. So, how can I check ?

Chandan
  • 11,465
  • 1
  • 6
  • 25
dokichan
  • 857
  • 2
  • 11
  • 44

2 Answers2

2

You can use should.deepEqual to match length and values with order

const arr = [
 'a',   'b',
 'c',   'd',
 'e',   'f',
 'g',   'h',
 'i',   'j'
];

arr.should.deepEqual(arr);
console.log("test passed");
<script src="https://cdnjs.cloudflare.com/ajax/libs/should.js/13.2.3/should.min.js"></script>
Chandan
  • 11,465
  • 1
  • 6
  • 25
0

If you need an exact match go with Chandan's answer, if you need to match a subset, you can use should.containDeep (see the docs). Here's an example:

const arr = [
 'a',   'b',
 'c',   'd',
 'e',   'f',
 'g',   'h',
 'i',   'j'
];

arr.should.containDeep(['a', 'b', 'c', 'd']);
console.log("test passed");
<script src="https://cdnjs.cloudflare.com/ajax/libs/should.js/13.2.3/should.min.js"></script>
eol
  • 23,236
  • 5
  • 46
  • 64