An introduction to assertions
Automated testing relies on the concept of assertions. Assertions themselves are quite simple: they are simply a statement of fact. If the statement is factually true, the test will pass (or will continue to the next step); but if the statement is false, the test will fail and report an error.
Note
The snippets in this section are located at snippets/06/ex1-basic-assertions/a
in the code package of this book. When using the interactive snippet playground, select 6: Testing and UI Automation and Example 1a.
Consider the following short snippet:
let x = 5; let y = x * 2;
We can make several assertions about this code after it has been executed:
x
will contain the value5
y
will contain the value10
Testing this manually isn't terribly difficult. We can open up a JavaScript REPL or a browser's debugging console and determine the truth of these assertions pretty easily. However, that's tedious to do on a continuous basis. We can do better than this. Let's add some code...