0

I have a unit test which checks the string body of a fetch() call.

However, this test is a bit fragile because if the object structure changes one is left trying to look at two giant strings and seeing what is different.

Instead I want to refine this test to check if the object produced out of calling JSON.parse() on this is equivalent to another object and possibly, chain this with checking for certain object sub properties.

    expect(global.fetch).toHaveBeenCalledWith(
    'http://localhost:8080/api',
    {
      "body": JSON.stringify(expected_body),
      "headers": {
        "Content-Type": "application/json"                                             
      },
      "method": "POST"                                                                 
    },                                                                                 
  );

Note this is not asking for a deep equal to comparison check. My question really is about how to call a lambda function before running the matcher.

What is the best way to test this?

Kartik Ayyar
  • 832
  • 1
  • 7
  • 23
  • @str this is not the same question for comparing an object being deep equal. This is really a question about how to call a lambda before running the matcher. – Kartik Ayyar Aug 13 '19 at 15:13
  • Then you should clarify that. Just changing the title is not enough. – str Aug 13 '19 at 16:54

1 Answers1

1

You should abstract away the http call into a separate module or function that returns an object that you define. This object would be created using the parsed JSON from the Lambda response. Then you can use a library like nock to mock your Lambda call in a unit test and create test cases for success and failure scenarios.

See https://www.npmjs.com/package/nock.

Victor P
  • 1,496
  • 17
  • 29