snapshooter icon indicating copy to clipboard operation
snapshooter copied to clipboard

Add option to ignore the order of elements in arrays when using Snapshot.Match

Open marionzr opened this issue 4 years ago • 5 comments

Is your feature request related to a problem? Please describe. I have the following json response and I want to ignore the order of the elements of metadata array. I'm using the Snapshooter.Xunit version 0.5.2 which allows me to ignore some fields (e.g. ids) but the order of the elements should be ignored as the elements will checked

Renerated response

{
  "data": {
    "createItem": {
      "item": {
        "id": 1,
        "name": "Dummy item",        
        "metadata": [
          {
            "id": 1,            
            "key": "answer",
            "value": "42"
          },
		  {
            "id": 2,
            "key": "question",
            "value": "the answer to life, the universe and everything"
          }
        ]
      }
    }
  }
}

Snap file

{
  "data": {
    "createItem": {
      "item": {
        "id": 1,
        "name": "Dummy item",        
        "metadata": [
          {
            "id": 1,
            "key": "question",
            "value": "the answer to life, the universe and everything"
          },
	  {
            "id": 2,            
            "key": "answer",
            "value": "42"
          }
        ]
      }
    }
  }
}

Describe the solution you'd like When matching arrays should be possible to set an option to ignore the order and the elements should be checks one by one to see if a matched element is found in the array.

Describe alternatives you've considered Allow injecting a function in the Snapshot.Match( to order the arrays before the comparison is executed

Additional context The failure message Message: Assert.Equal() Failure Expected: ··· "key": "question",\n Actual: ··· "key": "answer",\n

marionzr avatar Jun 09 '20 15:06 marionzr

I propose to have a additional match option, where you can define the order for an array. We order the array before the snapshot is created, so we can make sure, that the snapshots are always the same. We will discuss it next week in the team.

nscheibe avatar Jun 20 '20 09:06 nscheibe

Hi, I was wondering if this was implemented to ignore order of an array or if someone has a work around to currently ignore order when matching snapshots?

Aashay23 avatar Sep 15 '21 12:09 Aashay23

@Aashay23 afaik this has not been implemented jet. the current workaround is to sort the array yourself before making the snapshot.

RohrerF avatar Sep 15 '21 16:09 RohrerF

up!

christianweberr avatar Oct 12 '22 07:10 christianweberr

I recently had this same problem when running tests where the array ordering was tripping us up being able to use a Snapshooter

I have created an open source project for sorting JSON given some basic options and configuration that allowed for the use of Snapshooter in our use case and a few others that have come up before. I've decided to package it up as a nuget package. Robtest.JSorter if anyone fancies a try.

JSON reordering turns out to be quite an interesting topic as Arrays can be sequences and you may not want to sort them in certain circumstances but in others it doesn't matter.

I ended up writing an Integration test with Snapshooter as an example of using it in conjunction with Snapshooter -> Integration Test

robg-test avatar Aug 09 '23 14:08 robg-test