Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
QUnit plugin that adds assertions to deal with promises
JavaScript

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
.gitignore
.jshintrc
.travis.yml
Gruntfile.js
LICENSE
README.md
bower.json
index.html
package.json
qunit-promises.js

README.md

qunit-promises

QUnit plugin that adds assertions to check promises, availabe as qunit-promises on bower. Test page

endorse

Problem

Compared with other testing frameworks designed to deal with async code (see vows for example), there is way too much boilerplate code to check promises when using QUnit. For example, here is an asynchronous unit test that checks that a promise resolves successfully with a certain value:

// function delayedHello returns a promise

QUnit.asyncTest("test successful promise", 1, function (assert) {
    var promise = delayedHello();
    promise.then(function (actual) {
        assert.equal(actual, 'hello', 'promise resolved with "hello"');
    }).always(start);
});

Promises plugin

qunit-promises plugin adds a pair of methods to QUnit's assert object, making it very convenient to:

  • assert that the promise either resolves or is rejected
  • compare the final value to expected
  • restart the testing queue

Same test as above, rewritten using new assertion:

QUnit.asyncTest("test successful promise", 1, function (assert) {
    assert.willResolve(delayedHello(), 'hello', 'returns value "hello"');
});

API

assert.willResolve(fn, [expected value], [message])

- fn: function that returns a promise that should be resolved
- expected value (optional): to compare with the resolved value
- message (optional): to push in the log
assert.willReject(fn, [expected value], [message])

- fn: function that returns a promise that should be rejected (failed)
- expected value (optional): to compare with the rejected value
- message (optional): to push in the log

Install

Include qunit-promises.js after qunit.js, new methods are added to the assert object.

<script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
<script src="qunit-promises.js"></script>

For full example, see the test page.

Small print

Author: Gleb Bahmutov © 2013

License: MIT - do anything with the code, but don't blame me if it does not work.

Something went wrong with that request. Please try again.