Currently, I am in the process of creating unit tests for an API.
In a scenario where I implement the following code:
const apiResponse:object = JSON.parse(body)
expect(apiResponse).toHaveProperty('error')
If the API does not return JSON data, an error message is displayed:
SyntaxError: Unexpected token p in JSON at position 0 at JSON.parse ()
Instead of encountering errors during testing, I would prefer my test to fail if the JSON parsing fails.
I am looking for a jest test that can verify:
Can the string received be parsed as valid JSON?