Adding testing life cycle method in server.test.js file

The beforeEach method is going to let us run some code before every single test case. We're going to use beforeEach to set up the database in a way that's useful. For now, all we're going to do is make sure the database is empty. We're going to pass in a function, that function is going to get called with a done argument, just like our individual test cases are.

const {Todo} = require('./../models/todo');    

beforeEach((done) => { });

This function is going to run before every test case and it's only going to move on to the test case once we call done, which means we can do something asynchronous inside of this function. What I'm going to do is call Todo.remove, which is similar to the MongoDB native method. All we need to do is pass in an empty object; this is going to wipe all of our Todos. Then, we can tack on a then callback, and inside of the then callback we're going to call done, just like this:

beforeEach((done) => {
  Todo.remove({}).then(() => {
    done();
  })
});

Now, we can also shorten this using the expression syntax:

beforeEach((done) => {
  Todo.remove({}).then(() => done());
});

With this in place, our database is going to be empty before every request, and now our assumption is correct. We're assuming we start with 0 Todos, and we will indeed start with 0 Todos since we just deleted everything.