QUnit.module()


QUnit.module( name, lifecycle )

Description: Group related tests under a single label.

All tests that occur after a call to QUnit.module() will be grouped into that module, until other QUnit.module() is called. The test names will all be preceded by the module name in the test results. You can then use that module name to select tests to run.

You can specify code to run before and after each test using the lifecycle argument, and also to create properties that will be shared on the context of each test. Any additional properties on the lifecycle object will be added to that context. The callbacks can also be used to add properties.

Examples:

Example: Use the QUnit.module() function to group tests together:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
QUnit.module( "group a" );
QUnit.test( "a basic test example", function( assert ) {
assert.ok( true, "this test is fine" );
});
QUnit.test( "a basic test example 2", function( assert ) {
assert.ok( true, "this test is fine" );
});
QUnit.module( "group b" );
QUnit.test( "a basic test example 3", function( assert ) {
assert.ok( true, "this test is fine" );
});
QUnit.test( "a basic test example 4", function() {
assert.ok( true, "this test is fine" );
});

Example: A sample for using the setup and teardown callbacks

1
2
3
4
5
6
7
8
QUnit.module( "module A", {
setup: function() {
// prepare something for all following tests
},
teardown: function() {
// clean up after each test
}
});

Example: Lifecycle properties are shared on respective test context

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
QUnit.module( "Machine Maker", {
setup: function() {
this.maker = new Maker();
},
parts: [ "wheels", "motor", "chassis" ]
});
QUnit.test( "makes a robot", function( assert ) {
this.parts.push( "arduino" );
assert.equal( this.maker( this.parts ), "robot" );
assert.deepEqual( this.maker.made, [ "robot" ] );
});
QUnit.test( "makes a car", function( assert ) {
assert.equal( this.maker( this.parts ), "car" );
this.maker.duplicate();
assert.deepEqual( this.maker.made, [ "car", "car" ] );
});