dannyxnda
dannyxnda

Reputation: 1024

Jest compare arrays WITH ORDER

Everything which I've searched is always without/ignoring order.
I want to compare 2 arrays with order.

expect([1, 2, 3].concat(4)).toEqualWithOrder([1, 2, 3, 4])

expect([{ value: 1}, { value: 2}, { value: 3 }].concat({ value: 4 })
.toEqualWithOrder([
  { value: 1 },
  { value: 2 },
  { value: 3 },
  { value: 4 },
])

How can I do that with Jest? Should we use JSON.stringify?

Upvotes: 6

Views: 7670

Answers (1)

Bergi
Bergi

Reputation: 664969

Just use the normal .toEqual method. It does require the items to appear in the same order.

Only to deviate from that you'd need a special method.

Upvotes: 8

Related Questions