What's New in QUnit 1.16

Originally published at: http://www.sitepoint.com/whats-new-qunit-1-16/
Testing is an essential part of the workflow of every developer, or at least it should be. A survey of some years ago showed that about 50% of JavaScript developers don’t write tests at all, which is a little bit scary. A few months ago, I tried to encourage the practice of testing in JavaScript with a 3-parts series about QUnit, a JavaScript unit-testing framework. If you missed it, this series is made of the articles Getting Started with QUnit, How to Test Asynchronous Code with QUnit and QUnit Advanced Concepts: Modules and Configuration.

In December, version 1.16 of this framework was released with some important changes. In this article I’ll introduce you to them, and describe how they can affect your tests.

Preparing for Version 2.0

The first point you have to know about this version is that this will be the last before the next major version, QUnit 2.0, is released. Updating your tests to version 1.16 now will help you in the process of the migration to version 2.0. QUnit 1.16 introduces several new methods that will become the default in the next milestone, so planning now is a smart way to be prepared for the major release. Besides, as we’ll see in a few moments, in version 1.16 a couple of methods that you have used to test asynchronous code have been deprecated and some properties renamed, so you must be aware of the changes.

New Way to Test Asynchronous Code

Up to version 1.15, to test an asynchronous function you had to use a different method to define the test called QUnit.asyncTest(), and the QUnit.start() and QUnit.stop() methods. For example, let’s say that you had a function called max() that calculates the maximum from a given set of numbers, in QUnit 1.15 you could write a test like the following:

QUnit.asyncTest('max', function (assert) {
   expect(2);
   QUnit.stop(1);

   window.setTimeout(function() {
      assert.strictEqual(max(), -Infinity, 'No parameters');
      QUnit.start();
   }, 0);

   window.setTimeout(function() {
      assert.strictEqual(max(3, 1, 2), 3, 'All positive numbers');
      QUnit.start();
   }, 0);
});

Starting from version 1.16 QUnit.asyncTest(), QUnit.start(), and QUnit.stop() are deprecated. To define an asynchronous test you can use the same QUnit.test() method as you’d do for a synchronous test. The start/stop mechanism has been replaced by a new one that uses a new assertion method called async. The latter returns a unique resolution callback each time is invoked and this callback must be executed within the asynchronous operation in replacement of QUnit.start(). The callback returned from the QUnit.async() method can’t be executed twice because it’ll throw an error, so you don’t have to worry about that.

Rewriting the previous test according to the new mechanism results in the following code:

QUnit.asyncTest('max', function (assert) {
   expect(2);

   var done1 = assert.async();
   window.setTimeout(function() {
      assert.strictEqual(max(), -Infinity, 'No parameters');
      done1();
   }, 0);

   var done2 = assert.async();   
   window.setTimeout(function() {
      assert.strictEqual(max(3, 1, 2), 3, 'All positive numbers');
      done2();
   }, 0);
});

You can see the last snippet in action below and its code is also available as a JSFiddle:

Support for Promises

QUnit.test() can now automatically handle the asynchronous resolution of a Promise. Promise are an interesting pattern that has been used a lot in the last few years as a way to replace callbacks and to avoid what is known as the callback hell. Promises have been introduced natively in ECMAScript 6 and browsers are starting implementing this feature. If you need an introduction to promise, you can read the article JavaScript Promises – There and back again.
Continue reading this article on SitePoint

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.