Testing with Mocha, Chai, and Sinon
One of the great benefits of writing tests for your code is that you will be forced to think through how what you’ve written works. A test that is difficult to write might indicate code that is difficult to understand.
On the other hand, comprehensive coverage with good tests helps others (and you) understand how an application works. In this section, we’ll look at how to describe your tests using the test runner Mocha, using Chai as its assertion library, and Sinon when mocking is necessary to a test. We'll use redis to demonstrate how to create tests against a simulated dataset (rather than testing against production databases, which would, of course, be a bad idea). We’ll use npm as a test script runner.
To start, set up the following folder structure:
/testing /scripts /spec
Now, initialize a package.json
file with npm init
within the /testing
folder. You can just press Enter at the prompts, but when you are asked for a test command, enter the following...