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 project.
QUnit has a simple method to group tests into modules called QUnit.module(). Its syntax is as follows.
Method syntax: QUnit.module | |
---|---|
QUnit.module(name[, lifecycle]) | |
Group a set of related tests under a single module. | |
Parameters | |
name | (String) The name to identify the module. |
lifecycle | (Object) An object containing two optional functions to run before (beforeEach) and after (afterEach) each test, respectively. Each of these functions receives an argument called assert that provides all of QUnit’s assertion methods. |
Returns | |
undefined |
Looking at the signature of this method, you may guess how you can specify which tests belong to a given module. The answer is that the tests belonging to a module are those defined after QUnit.module() is invoked but before another call to QUnit .module() (if any) is found. The following code should clarify this statement:
QUnit.module('Core');
QUnit.test('First test', function(assert) {
assert.expect(1);
assert.ok(true);
});
QUnit.module('Ajax');
QUnit.test('Second test', function(assert) {
assert.expect(1);
assert.ok(true);
});
In this snippet we’ve highlighted in boldface the invocations of QUnit.module(). In this case the test labeled as “First test” belongs to the Core module, and the test labeled “Second test” belongs to the Ajax module.
If you put the previous code into action, you’ll see that the test names are preceded by the module name in the test results. In addition, you can select a specific module name to select the tests to run from the drop-down menu displayed in the top-right corner of the page. These details are illustrated in figure 14.4.
Figure 14.4. Execution of a test suite organized into modules

Now that you know how to organize a test suite in a proper and maintainable way, it’s time to learn some specific configuration properties of the QUnit framework.
Leave a Reply