Basic testing
In this section, you'll create your very first test case so that you can test whether your code is working as expected. By adding automatic testing to our project, we'll be able to verify that a function does what it says it'll do. If we make a function that's supposed to add two numbers together, we can automatically verify it's doing that. And if we have a function that's supposed to fetch a user from the database, we can make sure it's doing that as well.
Now to get started in this section, we'll look at the very basics of setting up a testing suite inside a Node.js project. We'll be testing a real-world function.
Installing the testing module
In order to get started, we will make a directory to store our code for this chapter. We'll make one on the desktop using mkdir
and we'll call this directory node-tests
:
mkdir node-tests
Then we'll change directory inside it using cd
, so we can go ahead and run npm init
. We'll be installing modules and this will require a package.json
file...