With jQuery Test Suite (jts) it's possible to do unit test for javascript. There are other libraries (e.g. JsUnit), but as we already use jQuery, this is the obvious choice.
A jts consists of a html-file and a set of javascript test files.
The html-file includes the test scripts, together with the jQuery- and
testrunner-libraries (+ a css file for styling). it also contains the necessary
markup for the tests to run on, and a container for the results.
The following markup file can be used as a template:
template.html (text/html, 817 bytes, info).
module('moduleName') is called on start of module test to prepend the module name to all tests. The tests itselves are contained in a test('testName', function)-method.
Example:
module("Module A");
test("Basic tests", function() {
expect(2);
ok( true, "This asserts to true" );
equals( "abc", "a" + "b" + "c", "These 2 things are equal" );
});
|
Method |
Description |
|---|---|
|
expect(asserts) |
Specify the number of expected assertions to guarantee that failed test (no assertions are run at all) don't slip through. |
|
reset() |
Resets the test setup. Useful for tests that modify the DOM. |
|
ok(a, msg) |
Asserts true. |
|
isSet(a, b, msg) |
Asserts that two arrays are the same |
|
isObj(a, b, msg) |
Asserts that two objects are equivalent |
|
equals(actual, expected, message) |
Checks that the first two arguments are equal, with an optional message.
|