Loading
Current section: Testing Asynchronous Code 5 exercises
Problem

Handling Promises in Tests

Transcript

00:00 We have a new function called "greetByResponse", and the purpose of this function is to accept a response, which is a Fetch API response object, read its body as JSON, and then pass the first name from that user object to the greet function that we already have. Since reading response bodies is an asynchronous operation, we await it,

00:18 which also forces us to mark this "greetByResponse" function as an async function. We already have a test written for this function right here that basically constructs a mock response and then passes this to this greetByResponse function, awaits the result, and asserts on the proper greeting message returned.

00:36 If we try to run this test, though, we will spot something peculiar. We can see that there is all three tests that we have, and they are marked as passing. However, right below this, there is a suspicious error that looks a lot like the failed assertion from here, because what's happening is that our test function

00:54 executes this callback, the actual test, without awaiting any asynchronous operations inside of the test. So it prints the test as passing straight away. However, the actual promise returned by the greetByResponse function then resolves to the string that doesn't match the string that we expect.

01:13 Your task for this exercise will be to adjust the test function to support asynchronous callbacks. And once you do, see if you can spot any issues with this test and resolve them.