最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest compare arrays WITH ORDER - Stack Overflow

programmeradmin2浏览0评论

Everything which I've searched is always without/ignoring order.
I want to pare 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?

Everything which I've searched is always without/ignoring order.
I want to pare 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?

Share Improve this question edited Oct 30, 2021 at 1:40 dannyxnda asked Oct 30, 2021 at 1:33 dannyxndadannyxnda 1,0441 gold badge11 silver badges37 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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.

发布评论

评论列表(0)

  1. 暂无评论