Skip to content

Instantly share code, notes, and snippets.

@Potherca
Last active February 18, 2026 10:51
Show Gist options
  • Select an option

  • Save Potherca/7946371 to your computer and use it in GitHub Desktop.

Select an option

Save Potherca/7946371 to your computer and use it in GitHub Desktop.
A basic qUnit test example https://gist.pother.ca/7946371/

qUnit Example

This gist contains the minimum needed to run qUnit tests and a example of the most common features you will want to get aquanted with when you start writing tests.

It is viewable online at https://gist.pother.ca/7946371/

Basic API Documentation

For the full API see https://qunitjs.com/api/

Main methods

  • QUnit.module( name, [options], [scope] ) Group related tests under a common label.

  • QUnit.start() Start the test runner.

  • QUnit.test( name, callback ) Define a test.

    • QUnit.test.each() Define tests using a data provider.
    • QUnit.test.if( name, condition, callback ) Define a test that is automatically skipped when a condition is false.
    • QUnit.test.only( name, callback ) Define a test that is exclusively run.
    • QUnit.test.skip( name, [callback] ) Define a test that will be skipped.
    • QUnit.test.todo() Define a test that is not yet expected to pass.

Assertions

  • assert.deepEqual( actual, expected, message = "" ) / assert.notDeepEqual( actual, expected, message = "" ) A strict and recursive (in)equal comparison.
  • assert.equal( actual, expected, message = "" ) / assert.notEqual( actual, expected, message = "" ) A non-strict (in)equality comparison.
  • expect( amount ) Specify how many assertions are expected in a test.
  • assert.false( actual, message = "" ) / assert.true( actual, message = "" ) A strict boolean comparison.
  • assert.ok( state, message = "" ) / assert.notOk( state, message = "" ) Check if the first argument is truthy / falsy.
  • assert.propContains( actual, expected, message = "" ) / assert.notPropContains( actual, expected, message = "" ) Check that an object does (not) contain certain properties.
  • assert.propEqual( actual, expected, message = "" ) / assert.notPropEqual( actual, expected, message = "" ) Compare an object’s own properties for (in)equality.
  • assert.rejects(promise, [expectedMatcher], message = "" ) Test if the provided promise rejects.
  • assert.strictEqual( actual, expected, message = "" ) / assert.notStrictEqual( actual, expected, message = "" ) A strict type and value comparison, checking for (in)equality.
  • assert.throws( blockFn, [expectedMatcher], message = "" ) Test if a callback throws an exception.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>QUnit Tests</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.25.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-2.25.0.js"></script>
<script src="tests.js"></script>
</body>
</html>
QUnit.config.reorder = false
/*
## Basic API Documentation
For the full API see https://qunitjs.com/api/
### Main methods
- `QUnit.module( name, [options], [scope] )`
Group related tests under a common label.
- `QUnit.start()`
Start the test runner.
- `QUnit.test( name, callback )`
Define a test.
- `QUnit.test.each()`
Define tests using a data provider.
- `QUnit.test.if( name, condition, callback )`
Define a test that is automatically skipped when a condition is false.
- `QUnit.test.only( name, callback )`
Define a test that is exclusively run.
- `QUnit.test.skip( name, [callback] )`
Define a test that will be skipped.
- `QUnit.test.todo()`
Define a test that is not yet expected to pass.
### Assertions
- `assert.deepEqual( actual, expected, message = "" )` / `assert.notDeepEqual( actual, expected, message = "" )`
A strict and recursive (in)equal comparison.
- `assert.equal( actual, expected, message = "" )` / `assert.notEqual( actual, expected, message = "" )`
A non-strict (in)equality comparison.
- `assert.false( actual, message = "" )` / `assert.true( actual, message = "" )`
A strict boolean comparison.
- `assert.ok( state, message = "" )` / `assert.notOk( state, message = "" )`
Check if the first argument is truthy / falsy.
- `assert.propContains( actual, expected, message = "" )` / `assert.notPropContains( actual, expected, message = "" )`
Check that an object does (not) contain certain properties.
- `assert.propEqual( actual, expected, message = "" )` / `assert.notPropEqual( actual, expected, message = "" )`
Compare an object’s own properties for (in)equality.
- `assert.rejects(promise, [expectedMatcher], message = "" )`
Test if the provided promise rejects.
- `assert.strictEqual( actual, expected, message = "" )` / `assert.notStrictEqual( actual, expected, message = "" )`
A strict type and value comparison, checking for (in)equality.
- `assert.throws( blockFn, [expectedMatcher], message = "" )`
Test if a callback throws an exception.
*/
const { module, test } = QUnit
// These tests are not included in a module
test('This is the name for this test', function (assert) {
assert.expect(1) // register how many assertions to Expect to be run within this test.
assert.ok(true) // No message, so will just display 'okay'
})
// is the same as
test('This is another test', function (assert) {
assert.expect(1)
// assertions to Expect to be run within this test.
assert.ok(true, 'Look, a message!')
})
//Declare a module to group tests together in
module('My Module')
// The following tests will be part of a module called 'My Module'
test('1-2-3', function (assert) {
assert.expect(3)
const one = 1, two = 2, three = 3
assert.equal(one, 1, '1 equals 1')
assert.equal(two, 2, '2 equals 2')
assert.equal(three, 3, '3 equals 3')
})
test('4-5-6', function (assert) {
assert.expect(3)
const four = 4, five = 5, six = 6
assert.equal(four, 4, '4 equals 4')
assert.equal(five, 5, '5 equals 5')
assert.equal(six, 6, '6 equals 6')
})
module('Testing for failure')
test('This test will catch an exception', function (assert) {
assert.expect(1)
assert.throws(function () {
throw new Error('Uh-oh!')
})
})
test('This test will catch a rejected Promise', async function (assert) {
assert.expect(1)
await assert.rejects(Promise.reject('Uh-oh!'))
})
module('All these tests will fail...')
test('This test will fail', function (assert) {
assert.expect(2)
assert.ok(false) // As `false` is not truthy, this will cause the test to fail
assert.equal(1, 2, 'Of course 1 does not equal 2!') // As will a failing comparison
})
test('This test will also fail', function (assert) {
assert.expect(1)
// Will yield: "Expected 1 assertions, but 0 were run""
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment