skip to Main Content

So im running into an issue where I have a response that contains an array of objects, the array isn’t always in the same order based off the response, but I need to verify that at least 1 of the objects in the array of objects contains a specific value (or key/value pair).

IE: pretend the json looks like this:

{
  data: [
    {
      foo: 'Name 1',
      bar: '2022-06-07T00:00:00',
    },
    {
      foo: 'Name 2',
      bar: '2022-06-07T00:00:00',
    },
    {
      foo: 'Name 3',
      bar: '2022-06-07T00:00:00',
    }

}

so I would so something like expect(responseBody.data).toContainEqual({foo: "Name 2"}) but that doesn’t work. Would something like toHaveProperty work better? (Still not sure I understand the difference between toEqual, toBe and toContainEqual etc… are they feel very similar.

edit: I suppose I need to add something, the object itself im checking may contain random data (like dates/timestamps/etc..) so I won’t know specifically ALL the exact data of the object.

2

Answers


  1. const responseBody = {
      data: [
        {
          foo: 'Name 1',
          bar: '2022-06-07T00:00:00',
        },
        {
          foo: 'Name 2',
          bar: '2022-06-07T00:00:00',
        },
        {
          foo: 'Name 3',
          bar: '2022-06-07T00:00:00',
        }
      ]
    };
    
    const desiredValue = 'Name 2';
    
    const containsDesiredValue = responseBody.data.some(obj => obj.foo === desiredValue);
    
    expect(containsDesiredValue).toBe(true);
    

    The above code checks whether any object in the array contains a specific value of "foo" key, using some method.

    Previous code that I wrote in JS:

    function checkIfObjectPresent(inputArray, matchObj) {
      const size = inputArray.length;
      
      for(let i = 0; i < size; i++) {
        let curr = inputArray[i];
        
        if(JSON.stringify(curr) === JSON.stringify(matchObj)) {
          return true;
        }
      }
      
      return false;
    }
    
    let data = [
      {
        foo: 'Name 1',
        bar: '2022-06-07T00:00:00',
      },
      {
        foo: 'Name 2',
        bar: '2022-06-07T00:00:00',
      },
      {
        foo: 'Name 3',
        bar: '2022-06-07T00:00:00',
      }
    ]
      
    let compWith = {
          foo: 'Name 1',
          bar: '2022-06-07T00:00:00',
    };
    
    let compWith2 = {
      fool: 'Tmp',
      bar: '2022-06-07T00:00:00',
    };
        
    console.log(checkIfObjectPresent(data, compWith)); // returns true
    console.log(checkIfObjectPresent(data, compWith2)); // returns false
    Login or Signup to reply.
  2. The most direct approach is to create an array of strings for the foo key, then use the .toContain() matcher to see if "Name 2" is in the array.

    expect(responseBody.data.map(e => e.foo)).toContain("Name 2"); 
    

    You can also write your own matcher but this may be overkill for this use case, especially if you don’t need a web-first assertion.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search