Testing a Mongoose model with Mocha

Mocha is one of the most popular testing frameworks for JavaScript; its main goal is to provide an easy way to test asynchronous JavaScript code. Let's install Mocha globally so that we can make it available to any Node.js application that we may develop in the future:

npm install -g mocha

We will also need an assert library that can be used together with Mocha. The Should.js library module is easy to use and it will be our choice, so let's install it globally too:

npm install -g should

Now that we have our testing modules installed, we need to specify our testcase file path in the package.json file, let's modify it by adding a test element pointing to Mocha and the testcase file in the script node:

{ "name": ...

Get RESTful Web API Design with Node.js - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.