An assertion is a function that validates a condition if the condition is not valid; it throws an error that causes the test function to fail. A test function can include one or more assertions; all the assertions have to pass in order to have the test function pass. In the first QUnit test example, we used the QUnit equal
and raises
assertions. In this section, the most important QUnit built-in assertions will be illustrated in more detail.
The ok
assertion takes two parameters. The first parameter is a condition and the second parameter is optional and represents the message that is to be displayed with the test. The ok
assertion is passed if the condition is true. For example, the following examples will pass:
ok(true, "true passes"); ok(4==4, "4 must equal 4"); ok("some string", "Non-empty string passes");