Over inside of server.test.js we can get started down at the very bottom by adding a describe block. I'm going to call describe, and this describe block will be named GET /todos/:id, and we can add our arrow function (=>) as the callback function. Inside of our describe callback we can now set up the test case that we're going to create together, it('should return todo doc'). This is going to be the test that makes sure that when we pass in a valid ID that does match a doc, the doc comes back. It's going to be an asynchronous test, so we will be specifying the done argument:
describe('GET /todos/:id', () => {
it('should return todo doc', (done) => {
});
});
Now, in order to run this test case we're going to need the ID of a Todo that's actually inside of the collection, and if you remember we do add two Todos to the collection, but unfortunately we don't have the IDs. The IDs are autogenerated behind the scenes; in order to fix this what we're going to do is add the ID property, _id. This means we'll be able to access the ID in our test case and everything will work as expected. Now, in order to do this we do have to load an ObjectID off of MongoDB, which we've done before. I'm going to make a constant using ES6 Destructuring. I'm going to grab ObjectID off of the return result from requiring mongodb:
const {ObjectID} = require('mongodb');
Now, inside of the todos array we can add an _id property for both of our two todos, new ObjectID(), with a comma—this is for the first todo—and down below we can add an _id for the second todo as well, new ObjectID():
const todos = [{
_id: new ObjectID(),
text: 'First test todo'
},{
_id: new ObjectID(),
text: 'Second test todo'
}];
Now that we have _ids and we can access those _ids by accessing them off the todos array, we're ready to write the test case.