Advanced Node.js Development
上QQ阅读APP看书,第一时间看更新

Setting up the test files

In Atom, inside my package.json file, I now have my devDependencies listed out:

Now, my output for this command might look a little different than yours. npm is caching some of my modules that I've installed recently, so as you can see in the preceding screenshot, it's just grabbing the local copy. They did indeed get installed though, and I can prove that by opening up the node_modules folder.

We're now going to create a folder inside the server where we can store all of our test files, and this folder is going to be called tests. The only file we're going to worry about creating for this section is a test file for server.js. I'm going to make a new file in tests called server.test.js. This is the extension we'll be using for test files in the chapter. Inside of the server.test file, we can now kick things off by requiring a lot of those modules. We're going to require the supertest module and expectThe mocha and nodemon modules do not need to be required; that's not how they're used.

 The const expect variable we'll get will be equal to require('expect'), and we'll do the exact same thing for supertest, using const:

const expect = require('expect');
const request = require('supertest');

Now that we have these in place, we need to load in some of our local files. We need to load in server.js so we have access to the Express app since we need that for super-test, and we also want to load in our Todo model. As you'll see a little later, we're going to be querying the database, and having access to this model is going to be necessary. Now the model already exports something, but server.js currently exports nothing. We can fix this by adding module.exports to the very bottom of the server.js file, setting it equal to an object. On that object, all we're going to do is set the app property equal to the app variable, using the ES6 object syntax.

module.exports = {app};

With this in place, we are now ready to load those two files in.