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 14.2 shows all the properties exposed via the QUnit.config object.
Table 14.2. The configuration properties of QUnit.config
Name | Description |
---|---|
altertitle | (Boolean) QUnit changes the document.title to add a check mark or an x to specify that a test suite passed or failed. Setting this property to false (the default value is true) disables this behavior. This is useful if your code works with document.title. |
autostart | (Boolean) QUnit runs tests when the load event is triggered on window. If you load tests asynchronously, you can set the value to false (by default it’s true) and then call QUnit.start() when everything is loaded. |
hidepassed | (Boolean) QUnit shows all the tests, including the ones passed. Setting this property to true, you’ll see only those that failed. |
moduleFilter | (String) Specify a single module to run by specifying its name. The default value is undefined, so QUnit will run all the loaded modules. |
reorder | (Boolean) The framework first runs tests that failed on a previous execution. If you want to change this behavior, set the value to false. |
requireExpects | (Boolean) Set this property to true if you want to force the use of the assert.expect() method. |
testId | (Array) This property allows QUnit to run specific test blocks by a hashed string combining their module name and test name. The default value is undefined. |
testTimeout | (Number) Set a maximum time execution after which all tests will fail. The default value is undefined, which means there’s no limit. |
scrolltop | (Boolean) Set this property to false if you want to prevent QUnit from going back to the top of the page when all the tests have executed. |
urlConfig | (Array) Controls the form controls to place into the QUnit toolbar (the bar where you can find the noglobals and notrycatch flags). By extending this array, you can add your own check boxes and select lists. |
The custom configuration of the test suite must be placed after the JavaScript file of QUnit. You can define the configuration in an external file, as in the following code, or inline it.
<script src="qunit-1.18.0.js"></script>
<script src="qunit-config.js"></script>
If you have a huge test suite, you may want to hide the passed tests by default to focus on those that have failed. To do that you use the hidepassed property described in table 14.2 as shown here:
QUnit.config.hidepassed = true;
If you want to force yourself or your team to specify the number of assertions, you can use the requireExpects property:
QUnit.config.requireExpects = true;
QUnit defines other methods that we haven’t and won’t cover in this lesson, but they’re of secondary importance. The topics we’ve covered up to this point allow you to create a complete test suite that’s sufficient for most cases.
In the next section, you’ll create a complete test suite to see all the QUnit knowledge you’ve acquired in action. It’ll be fun!
Leave a Reply