Adding seed data for the GET /todos test case

Now, in order to do that, the first thing that we're going to do is make up an array of dummy Todos. These Todos only need the text property since everything else is going to get populated by Mongoose. I can create a constant called todos, setting it equal to an array, and we're going to have an array of objects where each object has a text property. For example, this one could have a text of First test todo, and then I can add on a second object as the second item in the array, with a text property equal to Second test todo:

const todos = [{
  text: 'First test todo'
},{
  text: 'Second test todo'
}];

Now before we can actually write the test case, we have to modify beforeEach using a brand new Mongoose method called insertMany, which takes an array, as shown in the preceding code block, and inserts all of those documents into the collection. This means that we are going to need to tweak the code real quick.

Instead of having a simple arrow function that calls done, I'm going to tack on some curly braces and inside of the callback function, we're going to call Todo.insertMany and we're going to call insertMany with the array we defined in the preceding code block. This is going to insert all of the Todos in this array, our two Todos, and then we can do something like call done. I'm going to return the response, which is going to let us chain callbacks, and then I can tack on a then method, where I can use a really simple expression-based arrow function. All I'm going to do is call done using the expression syntax:

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

Now, let's go ahead and run the test suite. I'll warn you now, the other tests are going to have problems because the numbers that they assert are now going to be incorrect. Over in the Terminal, I'm going to start up the test suite by using the following command:

npm run test-watch

Once that test suite gets started, I'm going to move back into Atom and as you can see, as promised, both of the test cases failed. We expected 3 to be 1 and we expected 2 to be 0. Everything is now off by 2.

To fix this, we're going to use two different techniques. Inside the server.test.js file, in the Post todos test, for the first test, we're going to do is only going to find Todos where the text property equals to the Test todo text:

Todo.find({text}).then((todos) => {
expect(todos.length).toBe(1);
expect(todos[0].text).toBe(text);
done();
}).catch((e) => done(e));

This means that the resulting length is still going to be 1 and the first item should still have a text property equal to the text above. For the second test, we're going to leave the find call the way it is; instead, we're going to make sure that the length of the database is 2:

Todo.find().then((todos) => {
expect(todos.length).toBe(2);
done();
}).catch((e) => done(e));

There should only be two documents in the Todos collection because that's all we added, and this was testing a failure so a third one should not have been added. With this in place, you can see our two test cases are now passing:

We're now ready to move on and add a new describe block in the test case.