Building proper testing capabilities for a modern web application requires the following three main pieces:
Depending on which unit-testing library you select, you may also want to download additional tools. For instance, many libraries offer alternate styles of test reports, which must be downloaded and included separately. Moreover, if you use a library that lacks the ability to run tests at the command line, you may wish to add this functionality with a headless web browser such as the very popular PhantomJS, Doing so will make it easier to automate your tests so that they can, for instance, run periodically or in response to code checks-ins. While there are several different mocking libraries to choose from (we'll explain why you want one in a moment), we recommend using the most popular, Sinon.JS. Sinon is both powerful enough and easy enough to use, and thus, it's a great fit for just about any project. Similarly, there is really only one main option for acceptance testing and that is Selenium. We'll discuss Selenium at the end of the chapter, so for now, we will focus on the choice of a unit testing library.
There are far too many high-quality testing libraries, with far too many differences in features, to possibly cover them all properly in this book. However, we do need to choose a library for the examples in this chapter, and so, we've opted to use Mocha.js. Mocha is one of the newer libraries available as well as one of the most robust and powerful. Mocha can be used either with the traditional xUnit/TDD style used by libraries such as QUnit, or with the BDD style popularized (in JavaScript at least) by Jasmine. Further, while it can be run in the browser, it can also be run (by using PhantomJS) at the command line as well.
On top of all this, Mocha offers its users the choice of four different assertion libraries. While other major testing libraries include assertions as part of the library itself (which admittedly is more convenient: there is one less file to download), Mocha prefers to give developers a choice. Because there are subtle differences possible in the way assertions are implemented, Mocha lets you select the style so that you can have the style that most appeals to you.
However, while we will be using Mocha throughout this chapter, much of what we'll be demonstrating will in fact be similar (if not identical) to how many of the other popular testing libraries work.
3.94.202.151