Is there an Array equality match function that ignores element position in jest.js?

前端 未结 6 1779
栀梦
栀梦 2021-02-06 20:06

I get that .toEqual() checks equality of all fields for plain objects:

expect(
    {"key1":"pin         


        
相关标签:
6条回答
  • 2021-02-06 20:32

    Put the elements into a set. Jest knows how to match these.

    expect(new Set(["pink wool", "diorite"])).toEqual(new Set(["diorite", "pink wool"]));
    
    0 讨论(0)
  • 2021-02-06 20:34

    this does not answer the question exactly, but still may help people that end up here by google search:

    if you only care that a subset of the array has certain elements, use expect.arrayContaining() https://jestjs.io/docs/en/expect#expectarraycontainingarray

    e.g.,

    expect(["ping wool", "diorite"])
      .toEqual(expect.arrayContaining(["diorite", "pink wool"]));
    
    0 讨论(0)
  • 2021-02-06 20:40

    There is no built-in method to compare arrays without comparing the order, but you can simply sort the arrays using .sort() before making a comparison:

    expect(["ping wool", "diorite"].sort()).toEqual(["diorite", "pink wool"].sort());
    

    You can check the example in this fiddle.

    0 讨论(0)
  • 2021-02-06 20:51

    If you don't have array of objects, then you can simply use sort() function for sorting before comparison.(mentioned in accepted answer):

    expect(["ping wool", "diorite"].sort()).toEqual(["diorite", "pink wool"].sort());
    

    However, problem arises if you have array of objects in which case sort function won't work. In this case, you need to provide custom sorting function. Example:

    const x = [
    {key: 'forecast', visible: true},
    {key: 'pForecast', visible: false},
    {key: 'effForecast', visible: true},
    {key: 'effRegForecast', visible: true}
    ]
    
    // In my use case, i wanted to sort by key
    const sortByKey = (a, b) => { 
      if(a.key < b.key) return -1; 
      else if(a.key > b.key) return 1; 
      else return 0; 
      }
    
    x.sort(sortByKey)
    console.log(x)

    Hope it helps someone someday.

    0 讨论(0)
  • 2021-02-06 20:51

    You can combine using sets as stated in this answer with checking length of actual result and expectation. This will ignore element position and protect you from duplicated elements in the same time.

    expect(
      new Set(['pink wool', 'diorite'])
    ).toEqual(
      new Set(['diorite', 'pink wool'])
    );
    
    0 讨论(0)
  • 2021-02-06 20:52

    As already mentioned expect.arrayContaining checks if the actual array contains the expected array as a subset. To check for equivalence one may

    • either assert that the length of both arrays is the same (but that wouldn't result in a helpful failure message)
    • or assert the reverse: That the expected array contains the actual array:
    // This is TypeScript, but remove the types and you get JavaScript
    const expectArrayEquivalence = <T>(actual: T[], expected: T[]) => {
      expect(actual).toEqual(expect.arrayContaining(expected));
      expect(expected).toEqual(expect.arrayContaining(actual));
    };
    

    This still has the problem that when the test fails in the first assertion one is only made aware of the elements missing from actual and no the extra ones that are not in expected.

    0 讨论(0)
提交回复
热议问题