suchipi/chai-jest-snapshot

Feature Request: deep equal in any order for snapshots

Closed this issue · 2 comments

Hey there! It would be great to integrate an optional deep-equal-in-any-order functionality for snapshots as my CI fails now and then due to failing snapshots caused by this ordering issue. The data is actually correct and I don't care about the order.

Not sure if anyone else has come across this issue, but here's the current behaviour:

// test
const arrayOfObjects = [ { foo: "bar", id: 1 }, { foo: "bar", id: 2 }, { foo: "bar", id: 3 } ];

expect(arrayOfObjects).to.matchSnapshot();
// snapshot
exports[`should get objects`] = `
Object {
  "locations": Array [
    Object {
      "foo": "bar",
      "id": 1,
    },
    Object {
       "foo": "bar",
      "id": 2,
    },
    Object {
      "foo": "bar",
      "id": 3,
    },
`;
// change order of objects

 const arrayOfObjects = [
      { foo: "bar", id: 3 },
      { foo: "bar", id: 2 },
      { foo: "bar", id: 1 }
    ];

expect(arrayOfObjects).to.matchSnapshot(); 

// fails
Array [
         Object {
           "foo": "bar",
      -    "id": 3,
      +    "id": 1,
         },
         Object {
           "foo": "bar",
           "id": 2,
         },
         Object {
           "foo": "bar",
      -    "id": 1,
      +    "id": 3,
         },
       ]

Ideally, there would be an optional parameter to pass in such as matchSnapshot({ deepEqual: true }) that could potentially resolve this issue.

Thanks in advance for any replies/feedback!

I'm not interested in maintaining that functionality as part of this package; my goal with this package is only to enable using jest's snapshots via chai, and you would run into the same problem with jest. See here for more info. Could you use eg. lodash's sortBy to ensure the array you're passing in is sorted? Alternatively, you could use a Jest Snapshot Serializer to do the sorting; you can add one using the addSerializer API.

Thanks for the quick reply @suchipi and for the extra info !

You're right, sorting the data from the response before passing it in does make sense as a different approach.