Using assertion libraries in testing Node modules
In the previous sections, we made two test cases to verify that utils.add
and our utils.square
method work as expected. We did that using an if
condition, that is, if the value was not 44
that means something went wrong and we threw an error. In this section, we'll learn how to use an assertion library, which will take care of all of the if
condition in utils.test.js
code for us:
if (res !== 44) throw new Error(`Expected 44, but got ${res}.`) }
Because when we add more and more tests, the code will end up looking pretty similar and there's no reason to keep rewriting it. Assertion libraries let us make assertions about values, whether it's about their type, the value itself, whether an array contains an element, all sorts of things like that. They really are fantastic.
The one we'll be using is called expect. You can find it by going to Google and googling mjackson expect
. And this is the result we're looking for:

It's mjackson's repository...