Writing BDD-style tests with Mocha
From C# or Java, you may be most familiar with the xUnit-style of tests used by NUnit, JUnit, and so on. This style structures tests into classes, and turns method names into test names. This can be a bit restrictive, and isn't common in JavaScript testing. JavaScript test frameworks make use of the less structured, and more dynamic, nature of the language to allow more flexibility.
There are several different styles for writing tests in JavaScript. The most common is the so-called behavior-driven development (BDD) style in which we describe the behavior of our application in plain English. This is the default style of the most popular JavaScript testing frameworks. It is also common in frameworks for other programming platforms, most notably RSpec for Ruby.
We'll be using a popular test framework named Mocha. Let's first add this to our application:
> npm install mocha --save-dev
Note that --save-dev
adds Mocha to our package.json
file as a development...