QUnit

2.25.0 · active · verified Sun Apr 19

QUnit is a widely adopted, powerful, and easy-to-use JavaScript testing framework, providing a clear and efficient API for writing unit and integration tests. Originally developed to test the jQuery library, it has significantly matured into a versatile, standalone solution compatible with modern JavaScript applications across various environments, including Node.js (version 10+) and web browsers. The framework is currently stable at version 2.25.0, with an active release cadence that frequently introduces new features and improvements, often focusing on developer experience and compliance with modern JavaScript ecosystems. A significant upcoming release, 3.0.0-rc1, is already available, introducing native ES module (ESM) distribution, which is automatically utilized by Node.js when using `import` statements, marking a key modernization step for module resolution. QUnit's core philosophy centers on simplicity and clarity, which is evident in its direct assertion style, intuitive modular test organization with `QUnit.module()`, programmatic test filtering capabilities via `QUnit.config.testFilter`, and robust support for both synchronous and complex asynchronous testing patterns. It consistently focuses on clear test reporting and comprehensive TAP compliance, ensuring seamless integration with various Continuous Integration/Continuous Deployment (CI/CD) pipelines and test runners.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates defining a QUnit module, utilizing `beforeEach` and `afterEach` hooks, writing both synchronous and asynchronous test cases, and employing various QUnit assertion methods. This example is set up to run using native ES modules in a Node.js environment.

import QUnit from 'qunit';

QUnit.module('Calculator operations', function(hooks) {
  let sum;

  hooks.beforeEach(function(assert) {
    sum = 0;
    assert.step('beforeEach hook ran');
  });

  hooks.afterEach(function(assert) {
    assert.step('afterEach hook ran');
  });

  QUnit.test('should correctly add two numbers', function(assert) {
    sum = 5 + 3;
    assert.strictEqual(sum, 8, '5 + 3 should be 8');
    assert.ok(sum > 0, 'Sum should be positive');
    assert.verifySteps(['beforeEach hook ran']); // Verify beforeEach ran before assertions
  });

  QUnit.test('should handle asynchronous addition', function(assert) {
    const done = assert.async();
    setTimeout(() => {
      sum = 10 + 20;
      assert.strictEqual(sum, 30, '10 + 20 should be 30 after async wait');
      done();
    }, 50);
    assert.verifySteps(['beforeEach hook ran']);
  });

  QUnit.test('should handle zero gracefully', function(assert) {
    sum = 0 + 0;
    assert.strictEqual(sum, 0, '0 + 0 should be 0');
  });
});

// To run this example:
// 1. Install QUnit: `npm install qunit`
// 2. Save the code above as `test.js` in your project.
// 3. Add `"type": "module"` to your `package.json` to enable ESM in Node.js.
// 4. Run your tests from the terminal: `npx qunit test.js`

view raw JSON →