mesaugat / chai-exclude

Exclude keys to compare from a deep equal operation with chai expect or assert.
https://yarn.pm/chai-exclude
MIT License
34 stars 11 forks source link

Syntax for http response style JSON #25

Closed Torniojaws closed 5 years ago

Torniojaws commented 5 years ago

I'm not sure how the syntax should be for this case:

Input:

{
  "success": true,
  "data": [
    { id: 1, value: "Hello", created: "2019-10-02 14:30:00" },
    { id: 2, value: "There", created: "2019-10-02 14:30:00" },
    { id: 3, value: "General", created: "2019-10-02 14:30:00" }
  ]
}

Assert:

expect(res.body).excludingEvery('created').to.eql({
  "success": true,
  "data": [
    { id: 1, value: "Hello" },
    { id: 2, value: "There" },
    { id: 3, value: "General" }
  ]
});

I ended up separating the top level, but I was wondering if there is a way to exclude in a single assert? Perhaps something like .excludingEvery('data.created')?

Separated:

expect(res.body.success).to.eql(true);
expect(res.body.data).excludingEvery('created').to.eql([
    { id: 1, value: "Hello" },
    { id: 2, value: "There" },
    { id: 3, value: "General" }
]);
mesaugat commented 5 years ago

Perhaps something like .excludingEvery('data.created')?

@Torniojaws There's no way to do this currently. From what I see, you are doing it correctly by separating the assertions.

mesaugat commented 5 years ago

Also, @Torniojaws you can keep the created key in your result and chai-exclude will still ignore the key from comparison.

expect(res.body.data).excludingEvery('created').to.eql([
    { id: 1, value: "Hello", created: "2019-10-02 14:30:00" },
    { id: 2, value: "There", created: "2019-10-02 14:30:00" },
    { id: 3, value: "General", created: "2019-10-02 14:30:00" }
]);
mesaugat commented 5 years ago

Closing this issue @Torniojaws, I hope I have answered your question. If there are any updates in the future I'll reply back in this thread.