QUnit.test()


QUnit.test( name, callback )

Description: Add a test to run.

Add a test to run using QUnit.test().

The assert argument to the callback contains all of QUnit's assertion methods. Use this argument to call your test assertions.

QUnit.test() can automatically handle the asynchronous resolution of a Promise on your behalf if you return a thenable Promise as the result of your callback function.

Examples:

A practical example, using the assert argument and no globals.

1
2
3
4
5
6
7
8
9
10
QUnit.test( "a test", function( assert ) {
function square( x ) {
return x * x;
}
var result = square( 2 );
assert.equal( result, 4, "square(2) equals 4" );
});

An example of handling an asynchronous thenable Promise result. This example uses an ES6 Promise interface that is fulfilled after waiting 500ms.

1
2
3
4
5
6
7
8
9
10
QUnit.test( "a Promise-returning test", function( assert ) {
assert.expect( 0 );
var thenable = new Promise(function( resolve, reject ) {
setTimeout(function() {
resolve( "result" );
}, 500 );
});
return thenable;
});