Getting Started

The following guide will get you up-and-running with QUnit either in Node.js or in the Browser.

In Node.js

Getting started with QUnit for Node.js projects is quick and easy. First, install the qunit package using npm:

npm install --save-dev qunit

# Or, if using Yarn:
yarn add --dev qunit

Let’s create an example program that we can test! We’ll start with a function that adds two numbers. Create a file add.js with the following contents:

function add(a, b) {
  return a + b;
}

module.exports = add;

Now, let’s start writing tests! Create a file in a test directory, for example test/add.js, and write the following:

const add = require('../add.js');

QUnit.module('add');

QUnit.test('add two numbers', assert => {
  assert.equal(add(1, 1), 2);
});

This defines a test suite for the “add” feature, with a single test case that verifies the result of adding two numbers together.

You can now run the test suite through the QUnit CLI. It is recommended that you run the qunit command via an npm script, which will find the qunit command automatically from your local dependencies. In your package.json file, specify it like so:

{
  "scripts": {
    "test": "qunit"
  }
}

Then, simply run:

npm test

And QUnit will run your test!:

TAP version 13
ok 1 add > should add two numbers
1..1
# pass 1
# skip 0
# todo 0
# fail 0

Congrats! You just wrote and executed your first QUnit test!

Check out the API documentation to learn more about the QUnit APIs for organising tests and making assertions.

See Command-line interface for help with the qunit command.

Support policy

QUnit follows the Node.js Long-term Support (LTS) schedule and provides support for Current, Active LTS, and Maintenance LTS releases.

Package name prior to 2.4.1

Prior to QUnit 2.4.1, the npm package was published under the name “qunitjs” instead of “qunit”. To install earlier versions of QUnit for Node, check out qunitjs.

The 0.x and 1.x versions of the “qunit” package on npm holds an alternative CLI that is now published as node-qunit.

Linting

The eslint-plugin-qunit package has a variety of rules available for enforcing best testing practices as well as detecting broken tests.


In the Browser

To get started with QUnit in the browser, create a simple HTML file called test.html and include the following markup:

<!DOCTYPE html>
<meta charset="utf-8">
<title>Test Suite</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.16.0.css">
<body>
  <div id="qunit"></div>
  <div id="qunit-fixture"></div>
  <script src="https://code.jquery.com/qunit/qunit-2.16.0.js"></script>
</body>

That’s all the markup you need to start writing tests. Note that this loads the library from the jQuery CDN.

See also Integrations & Downloads for integration you can use to automate browser testing. These usually also manage the HTML file for you.

Let’s add the following script, which tests an example add() function for adding two numbers together:

<script>
  function add(a, b) {
    return a + b;
  }

  QUnit.module('add', function() {
    QUnit.test('should add two numbers', function(assert) {
      assert.equal(add(1, 1), 2);
    });
  });
</script>

This code defines a test module for the add() function and verifies the result of adding two numbers.

If you open this up in the browser you’ll find a detailed report of the tests that ran and their assertions, as well as various options for filtering and re-running individual tests to help during development. Like so:

Congrats! You just wrote and executed your first QUnit test!

Check out the API documentation to learn more about the QUnit APIs for organising tests and making assertions.

Browser support

QUnit currently supports the following browsers:

For older browsers, such as Internet Explorer 6-8, Opera 12+, or Safari 5+, please use the 1.x series of QUnit.

Integrations

The following integrations can be used to automate the running of browser tests with QUnit:

Example projects:

Release channels

These are the officially supported download channels for QUnit releases:


Further Reading