### Install Project Dependencies Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/README.md Installs all project dependencies specified in the Gemfile. ```shell bundle install ``` -------------------------------- ### Install Jekyll and Bundler Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/README.md Installs the required Ruby gems for building and deploying the QUnit project. ```shell gem install jekyll bundler ``` -------------------------------- ### Serve Project Locally Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/README.md Starts a local server to preview the QUnit project, typically for testing before deployment. ```shell bundle exec jekyll serve ``` -------------------------------- ### Disable Autostart with RequireJS Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/config/QUnit.config.md Demonstrates how to disable QUnit's autostart functionality and manually start tests after asynchronous module loading using RequireJS. ```js QUnit.config.autostart = false; require( [ "tests/testModule1", "tests/testModule2" ], function() { QUnit.start(); } ); ``` -------------------------------- ### propEqual Example Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/assert/propEqual.md Demonstrates the usage of the propEqual assertion to compare properties of two objects, even if they are instantiated from different constructors. ```javascript QUnit.test( "propEqual test", function( assert ) { function Foo( x, y, z ) { this.x = x; this.y = y; this.z = z; } Foo.prototype.doA = function () {}; Foo.prototype.doB = function () {}; Foo.prototype.bar = 'prototype'; var foo = new Foo( 1, "2", [] ); var bar = { x : 1, y : "2", z : [] }; assert.propEqual( foo, bar, "Strictly the same properties without comparing objects constructors." ); }); ``` -------------------------------- ### QUnit.start() Usage Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/QUnit/start.md Starts a QUnit test run when autostart is disabled. This is crucial for managing asynchronous tests, especially those with multiple asynchronous operations or callbacks. It's recommended to use assert.async for modern async testing patterns. ```javascript QUnit.config.autostart = false; require(["test/tests1.js", "test/tests2.js"], function() { QUnit.start(); }); ``` -------------------------------- ### QUnit Callbacks Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/_layouts/default.html Defines the callback functions that can be used to hook into various stages of the QUnit test lifecycle, such as test start, test done, and module start/done. ```APIDOC QUnit.begin( callback ) - Called when QUnit has finished running all tests. - callback: Function to execute. QUnit.done( callback ) - Called when all tests have completed. - callback: Function to execute, receives results object. QUnit.moduleStart( callback ) - Called when a new module begins. - callback: Function to execute, receives module name. QUnit.moduleDone( callback ) - Called when a module finishes. - callback: Function to execute, receives results object. QUnit.testStart( callback ) - Called when a test begins. - callback: Function to execute, receives test details. QUnit.testDone( callback ) - Called when a test finishes. - callback: Function to execute, receives test results. ``` -------------------------------- ### QUnit ok() Assertion Example Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/assert/ok.md Demonstrates the usage of the QUnit ok() assertion with various truthy and falsy values. The assertion passes if the provided state is truthy and fails otherwise. An optional message can be included for better test output. ```javascript QUnit.test( "ok test", function( assert ) { assert.ok( true, "true succeeds" ); assert.ok( "non-empty", "non-empty string succeeds" ); assert.ok( false, "false fails" ); assert.ok( 0, "0 fails" ); assert.ok( NaN, "NaN fails" ); assert.ok( "", "empty string fails" ); assert.ok( null, "null fails" ); assert.ok( undefined, "undefined fails" ); }); ``` -------------------------------- ### QUnit.extend Example Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/config/QUnit.extend.md Demonstrates the usage of QUnit.extend to modify an object, including updating existing properties, adding new ones, and removing properties by setting their value to undefined. ```javascript QUnit.test( "QUnit.extend", function( assert ) { var base = { a: 1, b: 2, z: 3 }; QUnit.extend( base, { b: 2.5, c: 3, z: undefined } ); assert.equal( base.a, 1, "Unspecified values are not modified" ); assert.equal( base.b, 2.5, "Existing values are updated" ); assert.equal( base.c, 3, "New values are defined" ); assert.ok( !( "z" in base ), "Values specified as `undefined` are removed" ); }); ``` -------------------------------- ### Logging with QUnit.dump.parse Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/config/QUnit.dump.parse.md Example demonstrating how to use `QUnit.dump.parse` within a `QUnit.log` callback to process and send data (actual and expected values) to PhantomJS via `sendMessage`. ```javascript QUnit.log(function( obj ) { // Parse some stuff before sending it. var actual = QUnit.dump.parse( obj.actual ); var expected = QUnit.dump.parse( obj.expected ); // Send it. sendMessage( "qunit.log", obj.result, actual, expected, obj.message, obj.source ); }); ``` -------------------------------- ### QUnit Configuration Options Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/config/QUnit.config.md This section details various configuration options for QUnit. These options allow developers to customize how QUnit runs tests, reports results, and handles exceptions. Options include controlling the document title, test execution start, collapsing failing tests, filtering tests, managing the test fixture, hiding passed tests, setting max diff depth, specifying modules to run, and handling global leaks. ```javascript QUnit.config.altertitle = true; QUnit.config.autostart = true; QUnit.config.collapse = true; QUnit.config.filter = "myTest"; QUnit.config.fixture = "
"; QUnit.config.hidepassed = false; QUnit.config.maxDepth = 5; QUnit.config.module = "myModule"; QUnit.config.moduleId = ["moduleId1", "moduleId2"]; QUnit.config.notrycatch = false; QUnit.config.noglobals = false; QUnit.config.seed = "randomSeed123"; ``` -------------------------------- ### QUnit throws Examples Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/assert/throws.md Demonstrates various ways to use the `assert.throws()` method in QUnit, including checking for specific error messages, error types, and validating errors with custom callback functions. ```javascript QUnit.test( "throws", function( assert ) { function CustomError( message ) { this.message = message; } CustomError.prototype.toString = function() { return this.message; }; assert.throws( function() { throw "error" }, "throws with just a message, not using the 'expected' argument" ); assert.throws( function() { throw new CustomError("some error description"); }, /description/, "raised error message contains 'description'" ); assert.throws( function() { throw new CustomError(); }, CustomError, "raised error is an instance of CustomError" ); assert.throws( function() { throw new CustomError("some error description"); }, new CustomError("some error description"), "raised error instance matches the CustomError instance" ); assert.throws( function() { throw new CustomError("some error description"); }, function( err ) { return err.toString() === "some error description"; }, "raised error instance satisfies the callback function" ); }); ``` -------------------------------- ### Example Usage of notDeepEqual Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/assert/notDeepEqual.md Demonstrates how to use the notDeepEqual assertion in a QUnit test to check if two objects are not deeply equal. ```javascript QUnit.test( "notDeepEqual test", function( assert ) { var obj = { foo: "bar" }; assert.notDeepEqual( obj, { foo: "bla" }, "Different object, same key, different value, not equal" ); }); ``` -------------------------------- ### QUnit Main Methods Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/_layouts/default.html Lists the primary methods available in the QUnit testing framework. These are the core functions used for setting up and running tests. ```APIDOC QUnit.module( name, [hooks,] callback ) - Groups related tests together. - name: The name of the module. - hooks: Optional object with setup and teardown functions. - callback: Function containing the tests for this module. QUnit.test( assert, [expected,] callback ) - Defines a single test case. - assert: An assertion object for making assertions within the test. - expected: Optional expected number of assertions. - callback: Function containing the test logic. QUnit.skip( assert, [expected,] callback ) - Skips a test case. - Parameters are the same as QUnit.test. QUnit.only( assert, [expected,] callback ) - Runs only the specified test case, skipping all others. - Parameters are the same as QUnit.test. ``` -------------------------------- ### Preconfigure QUnit Globally Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/config/QUnit.config.md Explains how to preconfigure QUnit by defining the global `QUnit` object with a `config` property before QUnit itself is loaded. This allows setting initial configuration values like `autoStart` and `noGlobals`. ```js // QUnit is not yet loaded here window.QUnit = { config: { autoStart: false, noGlobals: true, } }; ``` -------------------------------- ### QUnit.begin Callback Registration Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/callbacks/QUnit.begin.md Registers a callback function to be executed when the QUnit test suite begins. The callback receives an object containing the total number of tests. ```APIDOC QUnit.begin( callback ) Registers a callback to fire whenever the test suite begins. `QUnit.begin()` is called once before running any tests. Parameters: callback (function): Callback to execute. Provides a single argument with the callback details object. Callback details: callback( details: { totalTests } ) totalTests (number): The number of total tests in the test suite. Example: Get total amount of tests. ```js QUnit.begin(function( details ) { console.log( "Test amount:", details.totalTests ); }); ``` Using modern syntax: ```js QUnit.begin( ( { totalTests } ) => { console.log( `Test amount: ${totalTests}` ); }); ``` ``` -------------------------------- ### QUnit API Documentation Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/_layouts/wrapper.html This section details the various functions, methods, and configurations available within the QUnit API for writing and running tests. ```APIDOC QUnit API Documentation: This documentation covers the core components and functionalities of the QUnit testing framework. Core Modules: QUnit.module( name [, beforeEach, afterEach] ) - Defines a test module. - Parameters: - name: The name of the test module (string). - beforeEach: A function to run before each test in the module (optional). - afterEach: A function to run after each test in the module (optional). QUnit.test( assert, [expected], callback ) - Defines a single test. - Parameters: - assert: The assertion object provided to the test callback. - expected: The expected number of assertions in the test (optional). - callback: The function containing the test assertions. Assertions: assert.equal( actual, expected [, message] ) - Asserts that two values are strictly equal. - Parameters: - actual: The actual value. - expected: The expected value. - message: An optional message to display. assert.deepEqual( actual, expected [, message] ) - Asserts that two values are deeply equal. - Parameters: - actual: The actual value. - expected: The expected value. - message: An optional message to display. assert.ok( state [, message] ) - Asserts that the state is truthy. - Parameters: - state: The boolean state to check. - message: An optional message to display. Configuration: QUnit.config.autostart = true | false - Controls whether tests start automatically. QUnit.config.urlConfigEnabled = true | false - Enables or disables URL configuration. Hooks: QUnit.begin( callback ) - Callback executed when QUnit starts. QUnit.done( callback ) - Callback executed when all tests are completed. QUnit.log( details ) - Callback executed for each assertion. QUnit.moduleStart( details ) - Callback executed when a module begins. QUnit.moduleDone( details ) - Callback executed when a module finishes. QUnit.testStart( details ) - Callback executed when a test begins. QUnit.testDone( details ) - Callback executed when a test finishes. Example Usage: QUnit.module('Math Operations'); QUnit.test('Addition test', function(assert) { assert.equal( 1 + 1, 2, '1 + 1 should equal 2'); }); QUnit.test('Subtraction test', function(assert) { assert.equal( 5 - 3, 2, '5 - 3 should equal 2'); }); ``` -------------------------------- ### Test Lifecycle Hooks (before, beforeEach, afterEach, after) Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/QUnit/module.md Demonstrates the use of QUnit's test lifecycle hooks: `before`, `beforeEach`, `afterEach`, and `after`. These hooks allow you to set up and tear down test environments before and after tests or modules execute. ```javascript QUnit.module( "module A", { before: function() { // prepare something once for all tests }, beforeEach: function() { // prepare something before each test }, afterEach: function() { // clean up after each test }, after: function() { // clean up once after all tests are done } }); ``` -------------------------------- ### Limiting Output Depth with QUnit.dump.maxDepth Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/config/QUnit.dump.parse.md Demonstrates how to limit the output depth of `QUnit.dump.parse` by modifying the `QUnit.dump.maxDepth` property. The examples show the parsed output for `maxDepth` set to 1 and then 2. ```javascript var input = { parts: { front: [], back: [] } }; QUnit.dump.maxDepth = 1; console.log( QUnit.dump.parse( input ) ); // Logs: { "parts": [object Object] } QUnit.dump.maxDepth = 2; console.log( QUnit.dump.parse( input ) ); // Logs: { "parts": { "back": [object Array], "front": [object Array] } } ``` -------------------------------- ### QUnit.moduleStart Callback Registration Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/callbacks/QUnit.moduleStart.md Registers a callback function to be executed when a QUnit module begins. The callback receives an object containing the name of the module that is about to run. ```APIDOC QUnit.moduleStart( callback ) - Registers a callback to fire whenever a module begins. - Parameters: - callback (function): Callback to execute. Provides a single argument with the callback details object. - Callback details: - details: An object containing: - name (string): Name of the next module to run. - Example: QUnit.moduleStart(function( details ) { console.log( "Now running: ", details.name ); }); QUnit.moduleStart( ( { name } ) => { console.log( `Now running: ${name}` ); }); ``` -------------------------------- ### QUnit.stack API Documentation Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/config/QUnit.stack.md Provides details on the QUnit.stack method, including its parameters, return value, and usage. ```APIDOC QUnit.stack: __call__(offset = 0) description: Returns a single line string representing the stacktrace (call stack). parameters: - name: offset type: number description: Set the stacktrace line offset. Defaults to 0. returns: type: string | undefined description: A single line string representing the stacktrace, or undefined if not supported by the browser. notes: - Not all browsers support retrieving stacktraces. In those, QUnit.stack() will return undefined. example: QUnit.log( function( details ) { if ( details.result ) { // 5 is the line reference for the assertion method, not the following line. console.log( QUnit.stack( 5 ) ); } } ); QUnit.test( "foo", function( assert ) { // the log callback will report the position of the following line. assert.ok( true ); } ); ``` -------------------------------- ### QUnit Configuration Options Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/config/QUnit.config.md This section details various configuration properties available within QUnit.config, allowing customization of test execution, reporting, and user interface elements. ```APIDOC QUnit.config.storage (object) | default: sessionStorage Defines the storage object used to record failed tests between runs. The object must implement the Storage interface of the Web Storage API. Defaults to the global sessionStorage if defined. QUnit.config.reorder (boolean) | default: true By default, QUnit will run tests first that failed on a previous run. This can speed up testing but may lead to random errors if tests are not atomic. When a failed test is running first, 'Rerunning previously failed test' is displayed in the summary. QUnit.config.requireExpects (boolean) | default: false Requires each test to specify the number of expected assertions using the expect() method. Tests without expect() will fail if this is enabled. QUnit.config.testId (array) | default: undefined Allows QUnit to run specific tests identified by the hashed version of their module name and test name. Multiple tests can be specified. QUnit.config.testTimeout (number) | default: undefined Specifies a global timeout in milliseconds after which all tests will fail. Useful for preventing test runners from getting stuck on slow async tests. QUnit.config.scrolltop (boolean) | default: true Determines whether to scroll to the top of the page when the test suite is done. Setting to false leaves the page scroll position unchanged. QUnit.config.urlConfig (array) Controls which form controls are placed in the QUnit toolbar. Each element is an object with 'id', 'label', and optionally 'tooltip'. The 'value' property determines rendering: - undefined: Renders as a checkbox. URL parameter is 'true' when checked, absent otherwise. - string: Renders as a checkbox. URL parameter is the string when checked, absent otherwise. - array: Renders as a select-one with options from the array. URL parameter is absent for the empty option, otherwise set to the selected value. - object: Renders as a select-one with options from object properties. URL parameter is absent for the empty option, otherwise set to the selected property value. ``` -------------------------------- ### QUnit.testStart Callback Registration Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/callbacks/QUnit.testStart.md Registers a callback function to be executed at the beginning of each QUnit test. The callback receives a `details` object containing information about the test, such as its name, module, and ID. This is useful for logging or performing actions before a test runs. ```APIDOC QUnit.testStart( callback ) Registers a callback to fire whenever a test begins. Parameters: callback (function): Callback to execute. Provides a single argument with the callback details object. Callback details: callback( details: { name, module, testId, previousFailure } ) name (string): Name of the next test to run module (string): Name of the current module testId (string): Id of the next test to run previousFailure (boolean): Whether the next test failed on a previous run Example: QUnit.testStart(function( details ) { console.log( "Now running: ", details.module, details.name ); }); QUnit.testStart( ( { module, name } ) => { console.log( `Now running: ${module}: ${name}` ); }); ``` ```javascript QUnit.testStart(function( details ) { console.log( "Now running: ", details.module, details.name ); }); ``` ```javascript QUnit.testStart( ( { module, name } ) => { console.log( `Now running: ${module}: ${name}` ); }); ``` -------------------------------- ### Asynchronous Hooks with Promises Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/QUnit/module.md Shows how to handle asynchronous operations within QUnit hooks using Promises. This is useful for tasks like setting up or tearing down database connections or other async resources before/after tests. ```javascript QUnit.module( "Database connection", { before: function() { return new Promise( function( resolve, reject ) { DB.connect( function( err ) { if ( err ) { reject( err ); } else { resolve(); } } ); } ); }, after: function() { return new Promise( function( resolve, reject ) { DB.disconnect( function( err ) { if ( err ) { reject( err ); } else { resolve(); } } ); } ); } } ); ``` -------------------------------- ### Add Dropdown to Toolbar Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/config/QUnit.config.md Demonstrates adding a dropdown menu to the QUnit toolbar using `urlConfig`. This is useful for selecting different versions of libraries, such as jQuery, which can be checked by other parts of the application. ```js QUnit.config.urlConfig.push({ id: "jquery", label: "jQuery version", value: [ "1.7.2", "1.8.3", "1.9.1" ], tooltip: "What jQuery Core version to test against" }); ``` -------------------------------- ### QUnit.push Method Documentation Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/config/QUnit.push.md Details the deprecated QUnit.push method for custom assertions. It includes parameter descriptions, return values, and a strong recommendation to use assert.pushResult due to potential assertion leaks in asynchronous mode. ```APIDOC QUnit.push( result, actual, expected, message ) - DEPRECATED: Report the result of a custom assertion. - Parameters: - result (boolean): Result of the assertion. - actual: Expression being tested. - expected: Known comparison value. - message (string): A short description of the assertion. - Description: This method is deprecated and it's recommended to use [`pushResult`](/assert/pushResult/) on its direct reference in the assertion context. Invoking `QUnit.push` allows to create a readable expectation that is not defined by any of QUnit's built-in assertions. It may leak assertions in asynchronous mode. ``` -------------------------------- ### Add Checkbox to Toolbar Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/config/QUnit.config.md Illustrates how to add a new checkbox to the QUnit toolbar using the `urlConfig` property. This allows users to toggle a specific setting, like loading minified source files. ```js QUnit.config.urlConfig.push({ id: "min", label: "Minified source", tooltip: "Load minified source files instead of the regular unminified ones." }); ``` -------------------------------- ### QUnit.test() - Async/Await Support Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/QUnit/test.md Shows how QUnit.test() natively supports JavaScript async functions. Tests written with async/await will be correctly executed by QUnit, simplifying asynchronous test logic. ```javascript function squareAfter1Second(x) { return new Promise(resolve => { setTimeout(() => { resolve(x * x); }, 1000); }); } const { test } = QUnit; test( "an async test", async t => { var a = await squareAfter1Second(2); var b = await squareAfter1Second(3); t.equal( a, 4 ); t.equal( b, 9 ); t.equal( await squareAfter1Second(5), 25 ); }); ``` -------------------------------- ### QUnit Configuration Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/_layouts/default.html Details the configuration options available for customizing QUnit's behavior, such as test filtering, module grouping, and reporting. ```APIDOC QUnit.config.autostart = false - Prevents QUnit from starting tests automatically. Useful for manual control. QUnit.config.filter = 'test name' - Filters tests to run only those matching the provided string. QUnit.config.queue = true - Enables or disables the test queue, controlling test execution order. QUnit.config.reorder = false - Disables reordering of tests. By default, QUnit may reorder tests for better performance. QUnit.config.noglobals = true - Enables checking for undeclared global variables introduced during tests. ``` -------------------------------- ### Nested Modules with Callbacks Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/QUnit/module.md Illustrates how to create nested test modules using `QUnit.module()` with a callback function. This allows for more complex test structures and the organization of tests within sub-groups. ```javascript QUnit.module( "module a", function() { QUnit.test( "a basic test example", function( assert ) { assert.ok( true, "this test is fine" ); }); }); QUnit.module( "module b", function() { QUnit.test( "a basic test example 2", function( assert ) { assert.ok( true, "this test is fine" ); }); QUnit.module( "nested module b.1", function() { // This test will be prefixed with the following module label: // "module b > nested module b.1" QUnit.test( "a basic test example 3", function( assert ) { assert.ok( true, "this test is fine" ); }); }); }); ``` ```javascript const { test } = QUnit; QUnit.module( "module a", () => { test( "a basic test example", t => { t.ok( true, "this test is fine" ); }); }); QUnit.module( "module b", () => { test( "a basic test example 2", t => { t.ok( true, "this test is fine" ); }); QUnit.module( "nested module b.1", () => { // This test will be prefixed with the following module label: // "module b > nested module b.1" test( "a basic test example 3", t => { t.ok( true, "this test is fine" ); }); }); }); ``` -------------------------------- ### QUnit.test() - Basic Usage Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/QUnit/test.md Adds a test to run with a name and a callback function. The callback receives an assert object for making assertions. This is the fundamental way to define tests in QUnit. ```javascript function square( x ) { return x * x; } QUnit.test( "square()", function( assert ) { var result = square( 2 ); assert.equal( result, 4, "square(2) equals 4" ); }); ``` ```javascript function square( x ) { return x * x; } const { test } = QUnit; test( "square()", t => { t.equal( square( 2 ), 4, "square(2) equals 4" ); t.equal( square( 3 ), 9, "square(3) equals 9" ); }); ``` -------------------------------- ### Hooks Context and Nested Modules Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/QUnit/module.md Explains how test hooks (`beforeEach`, `afterEach`) share the same context (`this`) as their respective tests and how hooks stack on nested modules. This allows for shared state and predictable execution order. ```javascript QUnit.module( "Machine Maker", { beforeEach: function() { this.maker = new Maker(); this.parts = [ "wheels", "motor", "chassis" ]; } }); QUnit.test( "makes a robot", function( assert ) { this.parts.push( "arduino" ); assert.equal( this.maker.build( this.parts ), "robot" ); assert.deepEqual( this.maker.made, [ "robot" ] ); }); QUnit.test( "makes a car", function( assert ) { assert.equal( this.maker.build( this.parts ), "car" ); this.maker.duplicate(); assert.deepEqual( this.maker.made, [ "car", "car" ] ); }); ``` ```javascript QUnit.module( "grouped tests argument hooks", function( hooks ) { hooks.beforeEach( function( assert ) { assert.ok( true, "beforeEach called" ); } ); hooks.afterEach( function( assert ) { assert.ok( true, "afterEach called" ); } ); QUnit.test( "call hooks", function( assert ) { assert.expect( 2 ); } ); QUnit.module( "stacked hooks", function( hooks ) { // This will run after the parent module's beforeEach hook hooks.beforeEach( function( assert ) { assert.ok( true, "nested beforeEach called" ); } ); // This will run before the parent module's afterEach hooks.afterEach( function( assert ) { assert.ok( true, "nested afterEach called" ); } ); QUnit.test( "call hooks", function( assert ) { assert.expect( 4 ); } ); } ); } ); ``` -------------------------------- ### QUnit.module API Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/QUnit/module.md Defines a module to group related tests. Supports hooks for setup/teardown and nested modules. ```APIDOC QUnit.module( name [, hooks] [, nested ] ) - name (string): Label for this group of tests. - hooks (object): Callbacks to run during test execution. Properties: { before, beforeEach, afterEach, after }. - before (function): Runs before the first test. - beforeEach (function): Runs before each test. - afterEach (function): Runs after each test. - after (function): Runs after the last test. - nested (function): A callback used for nested modules. Nested module hooks: - hooks (object): Runs before the first test in a nested module. Description: Organizes tests into modules, allowing for selection and filtering. Test names are prefixed with the module name. Nested modules inherit hooks, forming queues for setup and stacks for teardown. Hooks can handle asynchronous Promises. The module callback is invoked with the test environment as its `this` context. ``` -------------------------------- ### QUnit.assert ok assertion Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/config/QUnit.assert.md Demonstrates the usage of the `ok` assertion method provided by the `assert` object, which is passed into the QUnit.test callback. This assertion checks if a given condition is truthy. ```js QUnit.test( "`ok` assertion defined in the callback parameter", function( assert ) { assert.ok( true, "on the object passed to the `test` function" ); }); ``` -------------------------------- ### Logging Stacktrace with QUnit.log Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/config/QUnit.stack.md Demonstrates how to use QUnit.stack within a QUnit.log callback to log the stacktrace of passing assertions. ```javascript QUnit.log( function( details ) { if ( details.result ) { // 5 is the line reference for the assertion method, not the following line. console.log( QUnit.stack( 5 ) ); } } ); QUnit.test( "foo", function( assert ) { // the log callback will report the position of the following line. assert.ok( true ); } ); ``` -------------------------------- ### QUnit.moduleDone API Documentation Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/callbacks/QUnit.moduleDone.md API reference for QUnit.moduleDone. This function allows developers to register a callback that is invoked upon the completion of a QUnit module. The callback receives a single argument, a details object, which contains properties like `name`, `failed`, `passed`, `total`, and `runtime`. ```APIDOC QUnit.moduleDone( callback ) Registers a callback to fire whenever a module ends. Parameters: callback (function): Callback to execute. Provides a single argument with the callback details object. Callback details: callback( details: { name, failed, passed, total, runtime } ) name (string): Name of this module failed (number): The number of failed assertions passed (number): The number of passed assertions total (number): The total number of assertions runtime (number): The execution time in milliseconds of this module ``` -------------------------------- ### QUnit assert.async() Usage Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/assert/async.md Demonstrates how to use assert.async() to manage asynchronous operations in QUnit tests. It covers single and multiple callback scenarios, including setting an expected call count. ```javascript QUnit.test( "assert.async() test", function( assert ) { var done = assert.async(); var input = $( "#test-input" ).focus(); setTimeout(function() { assert.equal( document.activeElement, input[0], "Input was focused" ); done(); }); }); ``` ```javascript QUnit.test( "two async calls", function( assert ) { assert.expect( 2 ); var done1 = assert.async(); var done2 = assert.async(); setTimeout(function() { assert.ok( true, "test resumed from async operation 1" ); done1(); }, 500 ); setTimeout(function() { assert.ok( true, "test resumed from async operation 2" ); done2(); }, 150); }); ``` ```javascript QUnit.test( "multiple call done()", function( assert ) { assert.expect( 3 ); var done = assert.async( 3 ); setTimeout(function() { assert.ok( true, "first call done." ); done(); }, 500 ); setTimeout(function() { assert.ok( true, "second call done." ); done(); }, 500 ); setTimeout(function() { assert.ok( true, "third call done." ); done(); }, 500 ); }); ``` -------------------------------- ### QUnit.todo API Documentation Source: https://github.com/qunitjs/api.qunitjs.com/blob/master/QUnit/todo.md Adds a test which expects at least one failing assertion during its run. Use this method to test a unit of code which is still under development (in a "todo" state). The test will pass as long as one failing assertion is present. If all assertions pass, then the test will fail signaling that QUnit.todo should be replaced by QUnit.test. ```APIDOC QUnit.todo( name, callback ) Adds a test which expects at least one failing assertion during its run. Parameters: name (string): Title of unit being tested callback (function): Function to close over assertions Callback parameters: callback( assert ): assert (object): A new instance object with the [assertion methods](/assert) Description: Use this method to test a unit of code which is still under development (in a "todo" state). The test will pass as long as one failing assertion is present. If all assertions pass, then the test will fail signaling that `QUnit.todo` should be replaced by `QUnit.test`. Example: How to use `QUnit.todo` to denote code that is still under development. ```js QUnit.module( "robot", { beforeEach: function() { this.robot = new Robot(); } }); // fireLazer hasn't been properly implemented yet, so this is a todo test QUnit.todo( "fireLazer returns the correct value", function( assert ) { var result = this.robot.fireLazer(); // Currently returns undefined assert.equal( result, "I'm firing my lazer!" ); }); ``` ```