Search code examples
javascriptnode.jssupertestshould.js

How to check if array contains defined values using supertest?


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 ?


Solution

  • 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>