Pluralsight blog Where devs, IT admins & creative pros go for news, tips, videos and more.
3,500+ tech & creative courses authored by experts - unlimited & online Get it now →
July 15, 2014

Top tools for unit testing in JavaScript

By javascript

Like many things in the world of JavaScript, there are plenty of choices for how to unit test your code. To help you save time when setting up a project, I’ve highlighted some of my favorites.

Why unit test your JavaScript?

If you don’t yet unit test your JavaScript, you might wonder why you’d want to test client-side code. If you’ve seen the tools coming out of client-side development lately, you know that client-side development is getting ever more interesting and complex, which means code quality standards (like unit testing), should be embraced by the front end.

The simple rule is this: If it has logic (we wrote ourselves), it needs tests. And since we’re putting more and more logic on our front end, we need tests! Unit tests make refactoring easier and ensure code quality. JavaScript is not exempt from this statement. Now, on with the tools.

Top tools

Jasmine: My favorite of the JavaScript unit testing frameworks, Jasmine, uses a behavior-driven development taxonomy where you describe a test suite, make it … should (or expect … to) expectations.

It’s very easy to get started in Jasmine, because if you’re new to unit testing in JavaScript, the authors conveniently have standalone distributions of Jasmine that can be run in a browser (via an HTML page that loads your scripts, the test runner and your test scripts).

A simple Jasmine test goes something like this:

describe(“My wonderful function”, function() {
    it(“should be very cool”, function() {
        expect(myfunction()).toEqual(“cool”);
    });
});

I’ve tried out other unit testing frameworks, and I keep coming back to Jasmine. It’s supported my other tools and test runners, and the describe format for tests make them easy to read (assuming you write them well).

Mocha: Mocha is an all-in-one testing tool that allows you to choose your preferred taxonomy (assert, expect, should), and a long list of features including notifications, coverage reporting, and more. It’s easily installed with npm, the node package manager, and you can install it globally to get some extra helpers.

One of the highlights (and criticisms) of Mocha is that it’s a bit of a “magical” framework. If you put your tests in a test/folder, mocha knows where they are. Running your test suite is as simple as running the “mocha” command.

And a fun feature: Among the many reporters provided, Mocha includes a nyan reporter. This lets you see the progress/results of your test suite (there are plenty of “regular” reporters too, such as a standard dot reporter, or a TAP-style reporter).

Karma: A “spectacular” test runner for JavaScript (the project was originally called Testacular), Karma is so easy to get up and running to do your JavaScript testing. Your test runner is important for your process, so hopefully you are or plan to eventually run your tests in a continuous integration environment to help ensure code quality.

Karma is easily installed with npm, and has a command line utility to help you set up your configuration. I run through the process on my blog, and it’s impressively easy to setup. If you plan to run your tests without a GUI browser, you can run them with PhantomJS, a headless WebKit browser (or you can use SlimerJS, a headless Gecko/Mozilla browser).

Other testing tools

As mentioned earlier, there are plenty of testing tools. I wanted to share my favorites, but it’s also good to know what else is out there.

For example, if you’re moving more toward a Node.js or JavaScript front and back end stack, and you find Mocha to be a little too magical, node-tap is a great, simple suite of tools becoming more popular in the Node community. Other unit testing frameworks include QUnit, which was inspired by the xUnit family of testing, and YUI Test, which is used at Yahoo!.

There are also other test runner options, such as Teaspoon (for Rails developers) or YUI Yeti. YUI Yeti in particular may be interesting, as it has XML output option for those running Jenkins for your continuous integration. Meanwhile, Teaspoon makes it very easy to integrate JavaScript testing in a Rails workflow.

This only covers unit testing tools. If you’re interested in end-to-end testing, or integration testing, I recommend checking out CasperJS for browser scripting. Keep your eye on Protractor if you’re interested in AngularJS. Protractor helps set-up a page with fake data to make it easier to test AngularJS (single-page) applications.

If we want to ensure code quality on the client-side, we need to be more rigorous on bringing our best practices for code quality along. Luckily, there are plenty of well-vetted tools available to do JavaScript unit testing (thanks in part to JavaScript also being a server side language now).

Did I miss any tools that you find useful? Let me know in the comments!

About the Author

is a web developer based in Philadelphia, PA. She speaks publicly anywhere from local user groups to international conferences on development topics including HTML/CSS, Ruby, Python, and JavaScript, and she also teaches web development and JavaScript, and can be reached via her blog at thewebivore.com or on Twitter @pamasaur.


Discussion