Testing RESTful APIs with Mocha
Have you noted that the app.js
express application created with express-generator
is actually a node.js
module exporting the express instance? In case you have, you must have asked yourself why that is actually needed. Well, having the express instance exported as a module enables it to be unit-tested. We already utilized the mocha
framework in Chapter 4, Using NoSQL Databases, where we developed a unit test for the CatalogItem
module. We will use mocha
once again and wrap a unit test around each operation API exposes. To unit-test the express application, we will need to do the following:
- Require an instance to the
express.js
application with the routes, making use of its being exported as a module - Start the
express.js
instance in unit test environment - Invoke its operations via a test library and assert against the results
- Finally, execute the
npm test
command to trigger the unit test
Before moving on and implementing mocha tests, we need a library for sending...