Current section: Test Structure 6 exercises

Create Test Blocks for Better Organization


00:00 I will implement this test function by first declaring a new function called test that accepts a title, which is a string, and a callback, which is a function that doesn't return anything. Inside this test function, I want to call the callback argument, which is the test itself. And if it executed successfully, I want to print this

00:18 with a little check icon and a test title in the terminal to indicate that. But if the callback threw an error, if there was assertion that throw, I will wrap it in the try catch block to catch those errors and report them again in the terminal. Now let's use this test function to wrap our existing tests.

00:37 So we will give them a meaningful test names that indicate our intention behind this code and move our existing expect calls within that test. With these changes, let's rerun the tests. And we can see the same failed assertion, but now there is a visual indicator and a test name

00:56 that points us to the exact test case that is failing. And at the same time, we can see a passing test for the congratulate function below. Because now, even though this expect line throws an error, the process doesn't exit because we catch that error in the test function right here and just forward it to the console. So with this, we can see that there is a problem with the greet function.

01:15 And if we go to its definition, we can quickly find a problem of broken string interpolation and fix it by using text string literals. We're running the tests, and now we can see them both passing.