snapshooter icon indicating copy to clipboard operation
snapshooter copied to clipboard

Ignore order of properties when comparing snapshots

Open vajsm opened this issue 1 year ago • 4 comments

Is your feature request related to a problem? Please describe.

Consider the scenario where tests are failing due to the exact same object being serialized differently (properties in a different order) on subsequent test runs, or when snapshots were created on one machine, and used in tests on another one.

The order of properties when serializing objects is not guaranteed by the framework (unless you're using [JsonProperty(Order = 1)]), and it caused me quite a headache when some tests in my project were failing when they were run in a different order. I don't have access to that codebase anymore, so I won't give a working (well, failing) example, but I was able to narrow it down to the fact that Snapshooter deserializes the string snapshots to JTokens inside of JsonSnapshotComparer.CompareSnapshots, and then serializes them back again to strings. This is the place where the order of properties gets changed, and as a result, the tests are failing because the strings aren't exactly the same.

If the objects were indeed identical, I believe the tests should pass. The way I see it, Snapshooter shouldn't rely on the actual order of JSON properties in the snapshot files by default, or at least should provide a possibility to configure such behavior.

Describe the solution you'd like

Snapshooter could order properties alphabetically when serializing/deserializing objects.

Describe alternatives you've considered

Additionally, there could be something like IgnorePropertyOrder(bool shouldIgnore) setting added to MatchOptions class, so that the order could be taken into consideration in scenarios when the order of properties matters for some reason.

Additional context

Let me know how you see it, and maybe I can help with the implementation - if you feel like it's a feature that should be added.

vajsm avatar Oct 25 '22 00:10 vajsm