or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

assertions.mdcli.mdconfiguration.mderror-handling.mdevents.mdhooks.mdindex.mdtest-definition.mdtest-flavors.mdutilities.md
tile.json

tessl/npm-qunit

The powerful, easy-to-use testing framework for JavaScript applications

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/qunit@2.24.x

To install, run

npx @tessl/cli install tessl/npm-qunit@2.24.0

index.mddocs/

QUnit

QUnit is a powerful, easy-to-use JavaScript testing framework that provides comprehensive testing infrastructure for both client-side and server-side JavaScript applications. Originally developed for the jQuery project, it has evolved into a standalone testing framework that supports Node.js, SpiderMonkey, and all major web browsers with zero dependencies.

Package Information

  • Package Name: qunit
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install qunit

Core Imports

// ES Modules
import QUnit from "qunit";

CommonJS:

// CommonJS
const QUnit = require("qunit");

Browser (global):

<script src="https://code.jquery.com/qunit/qunit-2.24.1.js"></script>

Basic Usage

import QUnit from "qunit";

// Define a test module
QUnit.module("Math Operations", function() {
  
  // Define a test
  QUnit.test("addition works correctly", function(assert) {
    assert.strictEqual(2 + 2, 4, "2 + 2 should equal 4");
    assert.ok(5 > 3, "5 is greater than 3");
  });
  
  // Test with setup and teardown
  QUnit.test("multiplication", function(assert) {
    assert.expect(2);
    assert.strictEqual(3 * 4, 12, "3 * 4 equals 12");
    assert.strictEqual(0 * 5, 0, "0 * 5 equals 0");
  });
});

// Start the test suite (if autostart is disabled)
QUnit.start();

Architecture

QUnit is built around several key components:

  • Test Definition API: Functions for defining individual tests and organizing them into modules
  • Assertion System: Comprehensive set of assertion methods for validating test conditions
  • Configuration System: Flexible configuration options for controlling test execution behavior
  • Event System: Event-driven architecture for monitoring test lifecycle and results
  • Reporter System: Built-in and extensible reporting system for different output formats
  • Hook System: Global and module-level hooks for setup and teardown operations
  • CLI Interface: Command-line tools for running tests in Node.js environments

Capabilities

Test Definition

Core functionality for defining and organizing tests into logical groups with support for various test types and execution modes.

/**
 * Define a test
 * @param {string} testName - Name of the test
 * @param {Function} callback - Test callback function
 */
QUnit.test(testName, callback)

/**
 * Define a test module
 * @param {string} name - Module name
 * @param {Function} [scope] - Module callback function
 */
QUnit.module(name, scope)

/**
 * Define a test module with options
 * @param {string} name - Module name
 * @param {Object} options - Module configuration
 * @param {Function} [scope] - Module callback function
 */
QUnit.module(name, options, scope)

Test Definition

Assertions

Comprehensive assertion library providing methods for validating test conditions, comparing values, and testing exceptions.

/**
 * Assertion methods available in test callbacks
 */
assert.ok(result, message)
assert.strictEqual(actual, expected, message)
assert.deepEqual(actual, expected, message)
assert.throws(block, expected, message)
assert.async(count)
assert.step(message)
assert.verifySteps(steps, message)
assert.timeout(duration)

Assertions

Configuration

Flexible configuration system for controlling test execution, filtering tests, setting timeouts, and customizing behavior.

/**
 * QUnit configuration object
 * @typedef {Object} QUnitConfig
 * @property {boolean} autostart - Whether to automatically start tests
 * @property {boolean} reorder - Whether to reorder tests based on previous failures
 * @property {boolean} requireExpects - Whether all tests must call expect()
 * @property {number} testTimeout - Default timeout for tests in milliseconds
 * @property {string} filter - Filter tests by name
 * @property {string} module - Filter tests by module name
 * @property {boolean} noglobals - Check for global variable pollution
 * @property {boolean} failOnZeroTests - Fail when no tests are run
 * @property {number} maxDepth - Maximum depth for object comparison
 */
QUnit.config

Configuration

Event System

Event-driven architecture enabling monitoring of test lifecycle, capturing results, and building custom reporting solutions.

/**
 * Register event listener
 * @param {string} eventName - Event name
 * @param {Function} callback - Event callback
 */
QUnit.on(eventName, callback)

// Available events: "runStart", "suiteStart", "testStart", "assertion", 
// "testEnd", "suiteEnd", "runEnd", "error"

Events

CLI Interface

Command-line interface for running tests in Node.js environments with extensive configuration options and built-in reporters.

// Command line usage:
// qunit [options] [files...]

/**
 * CLI Options
 * @typedef {Object} CLIOptions
 * @property {string} [filter] - Filter tests by pattern
 * @property {string} [module] - Run only specified module
 * @property {string} [reporter] - Specify reporter (console, tap)
 * @property {string[]} [require] - Modules to require before tests
 * @property {string} [seed] - Seed for test randomization
 * @property {boolean} [watch] - Watch files for changes
 */

CLI Interface

Hook System

Global and module-level hooks for setup and teardown operations, providing flexible test lifecycle management.

/**
 * Global hooks that apply to all tests
 * @param {Function} callback - Hook callback function
 */
QUnit.hooks.beforeEach(callback)
QUnit.hooks.afterEach(callback)

/**
 * Module-level hooks (available in module callback)
 * @param {Function} callback - Hook callback function
 */
hooks.before(callback)
hooks.beforeEach(callback)
hooks.afterEach(callback)
hooks.after(callback)

Hook System

Error Handling

Robust error and exception handling capabilities for managing uncaught errors and debugging test execution.

/**
 * Error handling functions
 * @param {Function} callback - Error callback function
 */
QUnit.onError(callback)
QUnit.onUncaughtException(callback)

/**
 * Push test failure
 * @param {string} message - Failure message
 * @param {string} [source] - Source location
 */
QUnit.pushFailure(message, source)

Error Handling

Utility Functions

Helper functions for object comparison, type checking, serialization, and debugging support.

/**
 * Utility functions
 */
QUnit.equiv(a, b)        // Deep equivalence check
QUnit.dump.parse(obj)    // Serialize object to string
QUnit.is(type, obj)      // Type checking
QUnit.objectType(obj)    // Get object type
QUnit.diff(a, b)         // Generate diff between values
QUnit.stack(offset)      // Get stack trace

Utility Functions

Types

Test Flavors

QUnit supports several test variants for different testing scenarios:

/**
 * Test variations
 */
QUnit.test(name, callback)      // Regular test
QUnit.skip(name)                // Skip test
QUnit.only(name, callback)      // Run only this test
QUnit.todo(name, callback)      // Todo test (expected to fail)

/**
 * Module variations
 */
QUnit.module(name, scope)       // Regular module
QUnit.module.skip(name, scope)  // Skip entire module
QUnit.module.only(name, scope)  // Run only this module
QUnit.module.todo(name, scope)  // Todo module

Test Flavors

Types

/**
 * @typedef {Object} ModuleOptions
 * @property {Function} [before] - Setup before module
 * @property {Function} [beforeEach] - Setup before each test
 * @property {Function} [afterEach] - Teardown after each test
 * @property {Function} [after] - Teardown after module
 */

/**
 * @typedef {Object} AssertionResult
 * @property {boolean} result - Whether assertion passed
 * @property {any} [actual] - Actual value
 * @property {any} [expected] - Expected value
 * @property {string} [message] - Assertion message
 */