sinon icon indicating copy to clipboard operation
sinon copied to clipboard

Behaviour of deep matching inconsistent between arrays and objects.

Open ashirley opened this issue 9 years ago • 5 comments

  • Sinon version : master

What did you expect to happen? Both of the following to return true:

sinonMatch({ deep: { str: "sinon" } }).test({ deep: { str: "sinon", ignored: "value" } })); //true sinonMatch([{ str: "sinon" }]).test([{ str: "sinon", ignored: "value" }])) //actual false, expected true

What actually happens

The first returns true (as expected), the second returns false.

To get the desired behaviour, the second line needs to be restated as

sinonMatch([sinonMatch({ str: "sinon" })]).test([{ str: "sinon", ignored: "value" }])) //false

i.e. explicitly nesting the matcher which seems counter intuitive.

How to reproduce

I have added some passing testcases to PR #1320 one of which (returns false if array is not equal (even if the contents would match (deep equal))) I think should be changed.

ashirley avatar Mar 08 '17 15:03 ashirley

I would agree with @ashirley that having to nest matches is counter-intuitive, and the only way I was able to figure out what was going on was to see this issue and adjust my approach

On the flip side, I didn't know that you could nest matches like this until I saw this issue, and this is likely going to fix an issue that I was having

mansona avatar Dec 20 '17 09:12 mansona

If sinonMatch encounters an expectation of type "object", it will use its own matchObject function, which only checks all the fields that are part of the expectation. If it encounters an object of type "array", it doesn't have a default handler and will instead let the expectation be handled by deepEqual, which is stricter as it checks the fields in both the expectation and the actual object.

rgroothuijsen avatar Feb 04 '18 20:02 rgroothuijsen

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 06 '18 16:04 stale[bot]

I still think this is a valid issue, how do I prevent it as being marked stale?

ashirley avatar Apr 10 '18 12:04 ashirley

I've added a "pinned" label, which tells stale bot to back off

mroderick avatar Apr 12 '18 15:04 mroderick