Frameworks for unit testing JavaScript

Do you know that joke, pretty famous among JavaScript developers, that says that you should think of a word, search Google for “<word>.js,” and if a library with that name exists, have a drink? If you didn’t, you know it now. The point of this joke isn’t to get you drunk but to highlight the huge number of JavaScript libraries, frameworks, and plugins out there. The same point could be made for unit testing frameworks.

The JavaScript community offers a lot of frameworks that you can use to unit test your projects. But like software, testing frameworks come and go. Before sticking with one you should check that it’s still maintained. In this section we’ll give you a brief overview of some of the most popular JavaScript unit-testing frameworks.

QUnit is the first unit-testing framework we want to introduce. It was developed to test jQuery, but then it turned into a standalone unit-testing framework. It has been adopted by all the other projects managed by the jQuery team, but it can be used with any JavaScript-based code. QUnit supports the same browsers as jQuery 1.x. One of the advantages of this framework is that it provides an easy-to-use set of methods that you can employ to test your project. In addition to the usual assert methods, QUnit allows you to test asynchronous functions.

Mocha is a feature-rich JavaScript test framework running on Node.js and the browser. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.

Jasmine is an open source, behavior-driven development (BDD) framework for JavaScript. It has a clean and easy-to-read syntax.

Behavior-driven development

Behavior-driven development is a software development process that evolved from test-driven development. When using BDD you not only test the code at the granular level with unit tests but also test the application using acceptance tests. BDD specifies that tests of any unit of software should be specified in terms of the desired behavior of the unit.

Other frameworks that you may read about on the web and that we want to mention are YUI Test and Selenium. Which framework to use depends on your preference, on your and your team’s skills, and on the kind of approach you want to adopt (TDD or BDD). In the remainder of this lesson we’ll discuss QUnit because, as we mentioned, it’s the framework maintained and used by the jQuery team. If they trust QUnit, why shouldn’t you? Let’s look at how you can start using it.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *