Litmus

Litmus Assertions

All litmus assertions are methods on the litmus.Test, which is the invocant (value of this) of the function passed to the constructor of the test. All assertion methods take a description of what is being tested as the last parameter, which should always be passed.

Simple Assertions

litmus.Test.pass(description)

This assertion always passes. This is typically used on a conditional, with a call to fail (see below) in the other branch of the conditional.

var desc = 'one is equal to one';

if (1 === 1) {
    this.pass(desc);
}
else {
    this.fail(desc);
}

The description for the pass and corresponding fail should be identical.

litmus.Test.fail(description)

This assertion always fails and is generally combined with a call to pass (see above).

litmus.Test.ok(cond, description)

Tests if cond evaluates to true in a boolean context.

this.ok(1 === 1, 'one is equal to one');

litmus.Test.nok(cond, description)

Tests if cond evaluates to false in a boolean context.

this.nok(1 === 2, 'one is not equal to two');

Comparison Assertions

litmus.Test.is(val, expected, description)

Tests if the first argument (val) is equal to the second (expected). The value is first checked using the == operator. If this fails, then a canonicalised string representation is formed for each value and these are compared - i.e. it works for data structures that contain the same values.

this.is(myModule.add(1, 1), 2, 'one plus one equals two');

litmus.Test.not(val, notExpected, description)

Tests if the first argument (val) is not equal to the second (notExpected). The same logic is applied to test for equality as for is above.

this.not(myModule.add(1, 1), 3, 'one plus one does not equal three');

litmus.Test.like(val, regex, description)

Tests if a value (val parameter) matches a regular expression (regex parameter).

this.like('valid', /^\w+$/, 'value is valid');

litmus.Test.unlike(val, regex, description)

Tests if a value (val parameter) does not match a regular expression (regex parameter).

this.unlike('not valid', /^\w+$/, 'value is not valid');

litmus.Test.gt(val, against, description)

Tests if a value (val parameter) is greater than another value (against parameter).

this.gt(2, 1, 'two is greater than one');

litmus.Test.gte(val, against, description)

Tests if a value (val parameter) is greater or equal to another value (against parameter).

this.gte(2, 1, 'two is greater or equal to one');

litmus.Test.lt(val, against, description)

Tests if a value (val parameter) is less than another value (against parameter).

this.lt(1, 2, 'one is less than two');

litmus.Test.lte(val, against, description)

Tests if a value (val parameter) is less or equal to another value (against parameter).

this.lte(1, 2, 'one is less or equal to two');

Advanced Assertions

litmus.Test.throwsOk()

Tests if a function raises an exception, and that exception matches a regular expression.

this.throwsOk(function () {
    throw new Error('expected exception');
}, /expected/, 'code throws as expected');

litmus.Test.isa(instance, expectedClass, description)

Tests if an object (instance parameter) is a member of a class (expectedClass parameter). The assertion passes if the object was created using the function expectedClass, or if it was created with any of the base classes (those that can be retrieved using the base property of expectedClass and the base property of that function, and so on).

this.isa(stimpy, Cat, 'Happy, Happy, Joy, Joy');