Setting up test scripts in package.json

Before we run the test, we're going to set up the scripts, just like we did in the test section. We're going to have two: test, which just runs the tests; and test-watch, which runs the test script through nodemon. This means that any time we change our app, the tests will rerun.

Right in test, we'll be running mocha, and the only other argument we need to provide is the globbing pattern for the test files. We're going to fetch everything in the server directory, which could be in a subdirectory (which it will be later), so we'll use two stars (**). It can have any file name, as long as it ends with the .test.js extension.

"scripts": {
  "test": "mocha server/**/*.test.js",
  "test-watch":
},

Now for test-watch, all we're going to do is run nodemon. We're going to be using the --exec flag to specify a custom command to run inside of single quotes. The command we're going to run is npm test. The test script on its own is useful, and test-watch simply reruns the test script every time something changes:

"scripts": {
  "test": "mocha server/**/*.test.js",
  "test-watch": "nodemon --exec 'npm test'"
},

There is still a major flaw we need to fix before we can move on. As you may have noticed, inside of the server.test file, we make a really big assumption. We assume that there's nothing already in the database. We assume this because we expect the Todos to be a length of 1 after adding 1, which means that we assumed it started at 0. Now this assumption is not going to be correct. If I were to run the test suite right now, it would fail. I already have Todos in the database. What we're going to do is add a testing life cycle method in the server.test file. This one is called beforeEach.