Loading
Current section: Principles 4 exercises
Problem

Write an Intentional Test

Transcript

00:00 We would like to greet any user who joins our app with a nice greeting message. So if John joins, he would say, "Hello, John." If Sarah, "Hello, Sarah." To do that, we have a greet function that accepts a name, argument, which is a string, and returns a greeting string, so "Hello, name." A very simple way to test this function is just call it.

00:19 So let's call greet and pass it a name, for example, "John." We can observe this result if we just console.log whatever this function returns. And run this greet.ts module in the terminal using npx tsx greet. So we can see that it prints, "Hello, John," which is exactly what we expect. But here's the problem.

00:40 This expectation really only lives in our head. It would be really nice to automate this process and also put this intention into code. So this will be a task for this exercise. Write a very minimal automated task, which consists basically of three steps, calling your tested code with particular input,

00:56 then taking its output and checking it against the expected result, and then if those two don't match, throwing an error. So give it a try, and see you in a bit.