0

I am writing some test cases for a function that takes in an input and provides a modified output object.

My mock response is :-

mockResponse = {
       "id":1,
       "value":23,
       "name":"first",
       "children":[
          {
             "id":2,
             "value":24,
             "name":"second",
             "children":[
                
             ]
          }
       ]
    }

This function gives me an output with different ids because I have a different logic for generation of ids in that function

 actualResponse =   {
       **"id": 2342342,**
       "value":23,
       "name":"first",
       "children":[
          {
             **"id":242,**
             "value":24,
             "name":"second",
             "children":[
                
             ]
          }
       ]
    }

I have many such mock objects that I need to compare with the result.

Is there a way I can ignore these ids while writing my jasmine test case ?

expect(actualResponse).toEqual(mockResponse);

Fow now I am spying on my uniqueId generator function and returning a hard coded value and using the same value as ids while forming the mock response object. But I believe there should be a better way to do this by ignoring the ids in the toEqual comparison.

EDIT 1:

I've used loadsh's isEqualWith to compare both the objects excluding the id's and saved the boolean response in a variable.

const areObjectsEqual = isEqualWith(mockResponse, actualResponse, (value1, value2, key) => {
            return key === 'id' ? true : undefined;
          });


expect(areObjectsEqual).toBeTrue();  //Successful test case. 

Do you believe this way of comparing instead of relying on jasmine and Karma's toEqual a bad practice?

Also, will this have an impact on code coverage?

Veryon890
  • 330
  • 1
  • 4
  • 15
  • I know I can use isEqualWith of lodash and ignore the ids while comparison and use the response of isEqualWith to check for falsy or truthy values in the expect statement. But I still believe there could be a better way to handle this in jasmine . – Veryon890 Jun 09 '21 at 11:15
  • Maybe you can try the suggestions here: https://stackoverflow.com/a/24487384/7365461 – AliF50 Jun 09 '21 at 12:58
  • @AliF50 I went through it and I am not quite sure how I can apply it to this condition. My object has a lot of properties and some are even dynamic, I only need to exclude id from it. Do you mind explaining how I leverage "objectContaining" and use it in my scenario. – Veryon890 Jun 09 '21 at 13:28
  • Yes, you're right. `id` being in the nested `children` makes it difficult. I am not sure how to go about doing it other than manually removing `ids` from both your `expected` and `actual` and then doing a `.toEqual` on the two. – AliF50 Jun 09 '21 at 13:35
  • @AliF50 I posted an edit to this question, I've used loadash's isEqualWith to omit ids and provide a boolean result. Could you check the Edit regarding my concerns. and provide any info if possible. Thanks! – Veryon890 Jun 09 '21 at 16:12
  • 1
    That looks good and is a nice solution. I don't think it will affect code coverage but you can check your code coverage to see if it was affected. – AliF50 Jun 09 '21 at 16:19

0 Answers0