Category: Unit testing with QUnit

  • Conclusion

    In this lesson we described the fundamental concepts of software testing and why unit-testing your code is so important. Testing gives you more confidence that your code is working properly and that it (virtually) doesn’t have bugs. We provided an overview of the frameworks available for unit testing JavaScript projects, focusing our attention on QUnit.…

  • An example test suite

    In this last section you’ll build a complete test suite for a project, and what’s better than testing something you’ve developed in this very app? That’s why you’ll create a test suite for Jqia Context Menu, the jQuery plugin you built in lesson 12 to show a custom context menu on one or more specified elements of…

  • Configuring QUnit

    In the same way that jQuery has a lot of reasonable default values for many of its methods, QUnit is released with a preset configuration. Sometimes you may need to tweak this configuration a bit to satisfy your project-specific needs. The framework allows you to override these default values, exposing them through a property called config. Table…

  • Group your tests in modules

    As a plugin or library increases in size, you may want to split the source into modules to enhance its maintainability. You already saw this pattern when we discussed the structure of jQuery, whose code is made up of more than 10,000 lines. The same principle applies to the code you write to test your…

  • Noglobals and notrycatch

    QUnit offers you two check boxes, noglobals (labelled as “Check for Globals”) and notrycatch (labelled as “No try-catch”), that you can check and uncheck as needed in order to change the behavior of all tests performed on the page. The noglobals flag will fail a test if a new global variable (which is the same as adding a property to the window object)…

  • How to test asynchronous tasks

    Sometimes you need to perform a given action or repeat it over and over again after a given amount of time. Other times you want to retrieve information from a server without reloading the page. These are situations where you need to execute one or more functions asynchronously. To test asynchronous functions you can use…

  • The throws() assertion method

    We left the throws() assertion method to the end because it’s a bit different from the others. Its syntax is as follows. Method syntax: throws throws(function[, expected][, message ]) Verify that a callback throws an exception, and optionally compare the thrown error. Parameters function (Function) The function to execute. expected (Object|Function|RegExp) An Error object, an Error function…

  • Testing your code using assertions

    Assertions are the core of software testing because they allow you to verify that your code is working as expected. QUnit provides numerous methods to test your expectations that can all be accessed within a test through the assert parameter passed to the function passed to QUnit.test(). We’ll start our overview of the assertion methods by covering four…

  • Creating tests for synchronous code

    QUnit allows you to test synchronous and asynchronous code. For the moment we’ll focus on testing synchronous code because it’s the easiest way to delve into the world of QUnit. To create a test in QUnit you have to use a method called test(). Its syntax is shown here. Method syntax: QUnit.test QUnit.test(name, test) Add a…

  • Getting started with QUnit

    One of the best features of QUnit is its ease of use. Getting started with this framework is a matter of performing three simple steps. The first thing to do is to download the framework. QUnit can be downloaded in several different ways. The first method is to access its website and download the JavaScript…