10 trusty JavaScript test tools

From simple test runners to complete testing frameworks, look to these proven tools to keep your JavaScript free of bugs

10 better JavaScript test tools
Getty Images

The need to test JavaScript code is straightforward. How else to keep out the bugs and make sure applications run smoothly in the browser or on Node.js? Fortunately, developers have a plethora of options when it comes to JavaScript testing.

Test runners and frameworks for unit testing, integration testing, and functional testing are readily available for the JavaScript ecosystem, with coverage for Node.js and browsers as well as for the Angular and React frameworks. Following are 10 of the many noteworthy options JavaScript developers have to help them test their apps.

AVA

AVA is a test runner for Node.js that offers a concise API, detailed error output, and support for new language features and process isolation. AVA is best-suited for testing Node.js modules and server applications, but perhaps not for testing UI applications. Among its capabilities, AVA offers a feature to mark a test as a “to do” task. Each test file run is a separate process. Other benefits cited include AVA’s minimalism and speed, simple test syntax, and ability to run tests concurrently. AVA also has support for async functions and observables—an observable being a type that can be used to model push-based data sources such as DOM events.

Install AVA

Instructions for installing AVA can be found on the project’s GitHub page.

Cucumber.js

The JavaScript implementation of the Cucumber test tool, Cucumber.js runs on maintained Node.js versions and in modern web browsers. The Cucumber project cites benefits including team communications and the ability to run automated tests written in “plain” language, meaning anyone on the team can read them. Thus, communication, collaboration, and trust can be improved. Cucumber.js includes an executable file to run your test suites (called features), so does not work when installed globally. (Cucumber must be required in support files and globally installed modules cannot be required.)

Install Cucumber.js

Instructions for installing Cucumber.js are available on GitHub.

Enzyme

Enzyme is a testing utility for the React JavaScript UI library. It is intended to make it easier to test the output of React components. Developers can manipulate, traverse, and simulate runtime given the output. The Enzyme API mimics the jQuery API for DOM manipulation and traversal. Enzyme is unopinionated about the test runner or assertion library. If developers want to use Enzyme with custom assertions and convenience functions to test React components, they can consider chai-enzyme with Mocha/Chai, jasmine-enzyme with Jasmine, or jest-enzyme with Jest. Enzyme also can be used to test React Native components.

Install Enzyme

Instructions for installing Enzyme can be found on GitHub. 

Karma

Karma is a test runner for JavaScript that enables execution of code in multiple browsers. All the major browsers, including mobile browsers, are supported. The developers behind the project say Karma offers an environment where developers do not have to set up loads of configurations, but can write code and get instant feedback from tests. Karma is designed for low-level (unit) testing. Not a testing framework or an assertion library, Karma launches an HTTP server and generates a test runner file from the developer’s favorite testing framework. Plug-ins are offered for frameworks such as Jasmine, Mocha, and QUnit.

Install Karma

Instructions for installing Karma are available on GitHub.

Jasmine

Jasmine is billed as a “behavior-driven” framework for testing JavaScript. It has no dependence on other JavaScript frameworks and does not need a DOM. Its proponents cite its syntax as enabling easy writing of tests. Maintained by Pivotal Labs, Jasmine is suited for testing websites, Node.js projects, and anything else where JavaScript can run. Jasmine is intended to bring ease of testing to a variety of different use cases and browsers and is JavaScript framework agnostic, allowing developers able to test code from React or Angular or any other JavaScript library. Jasmine strives for minimal dependencies while staying small and maintainable.

Install Jasmine

Instructions for installing Jasmine and getting started with Jasmine can be found on GitHub.

Jest

Jest is billed as a comprehensive JavaScript testing solution that works out of the box, configuration-free, for most JavaScript projects. Tests have unique global state and can be run in parallel. Previously failed tests are run first, with runs reorganized based on the length of the test files. Just works with popular JavaScript technologies including Angular, Babel, Node.js, React, and Vue. Tests can be run to keep track of large objects, with snapshots living alongside tests or embedded inline. In test files, Jest puts methods and objects into the global environment, with no need to import them.

Install Jest

Instructions for installing Jest can be found on GitHub. 

Luna

Luna is an opinionated unit testing framework for JavaScript that requires no configuration. Developers can have their first unit tests running within minutes, the product documentation states. Code coverage reports are generated without installing any other modules. Tests must be written as ES6 modules, with tests run in the browser by default. Concurrency is supported, with test groups run in parallel. Luna was partially inspired by the built-in testing available for Google’s Go language. Luna does not accommodate transpiling, so will not work with CoffeeScript or TypeScript. It also lacks support for older browsers. 

Install Luna

Instructions for installing Luna are available on GitHub. 

Mocha

A feature-rich test framework that runs on Node.js and in the browser, Mocha promises to make testing asynchronous code “simple and fun.” Tests are run serially, which proponents say allows for accurate, flexible reporting while mapping uncaught exceptions to accurate test cases. For continuous testing, developers can use the Wallaby.js tool to enable real-time code coverage for Mocha with any assertion library. Mocha integrates with application frameworks as well, such as through Konacha, which lets developers use Mocha to test JavaScript in Ruby on Rails applications. A number of editor plug-ins are available as well, such as the Mocha sidebar extension for Visual Studio Code. 

Install Mocha

Instructions for installing Mocha and documentation for Mocha’s many features are available on the project website. 

Protractor

Protractor is an end-to-end testing framework for Angular and its predecessor, AngularJS. Tests are run against applications in a browser, with Proctractor interacting with an application just as a user would. Angular-specific locator strategies are supported, for testing Angular elements without any setup. An automatic waiting capability enables Protractor to execute the next step in a test the moment a webpage completes pending tests. Protractor was built on top of WebdriverJS, a JavaScript implementation of the Selenium browser automation tool used for testing.

Install Protractor

Instructions for installing Protractor can be found on the Protractor website. 

QUnit

QUnit is a JavaScript unit test framework that is used by the JQuery, jQuery UI, and jQuery Mobile projects. In fact QUnit was originally developed as part of jQuery by jQuery inventor John Resig. QUnit is billed as fast and easy to use, with an API that is opinionated but lean and extensible, and it can be used to test any generic JavaScript code. When a test or assertion fails, QUnit promises to provide feedback as quickly as possible, with sufficient detail to figure out the issue. Assertion methods in QUnit follow the CommonJS unit testing specification. QUnit supports browsers backed by jQuery 3.x, including variants of Chrome, Edge, Firefox, Internet Explorer, and Safari.

Install QUnit

You can download and install QUnit from the QUnit website or from the jQuery.cdn

Copyright © 2019 IDG Communications, Inc.