9

I created unit (async) test in Jest. But when I get response from server:

[
    {
        name: "My name"
    },
    {
        name: "Another name"
    }
]

and test it:

test('Response from server', () => {
    get('my-url').end(error, response) => {
        expect(response.body).toBe(expect.any(Array))
    }
})

some error occurs:

Comparing two different types of values. Expected Array but received array.

It's working when I use expect(response.body).any(Array). But is there any fix for expect.toBe()?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Vesmy
  • 1,190
  • 4
  • 15
  • 29
  • `expect(response.body).any(Array)` shouldn't work. `.any` doesn't exist on the response from `expect()` but on `expect` itself. The referenced code gives this error: "_TypeError: expect(...).any is not a function_". – Samuel Neff Sep 01 '21 at 14:04

1 Answers1

16

You should use toEqual (not toBe) to compare objects and arrays. Use toBe for scalar data types only. If you like to check the response data type use typeof operator

Alexander Elgin
  • 6,796
  • 4
  • 40
  • 50