Assertions

Within each test, you can use any of these functions to check the output of your program. If an assertion fails, no further code in that test is run.

You are free to use any other assertion library you like; as long as your assertions throw errors when invalid, those errors will be caught and reported by jstest. To make jstest report these errors as failures, add the type of error your assertion library throws to this list:

JS.Test.ASSERTION_ERRORS.push(assert.AssertionError);

(jstest treats AssertionError from the Node assert module as a failure out of the box.)

The assertions provided by jstest are listed below.

assertBlock([message, ]callback)

Passes if the callback function returns a truthy value. message is an optional parameter, as it is for all other assertions, and if given should be a string that will be used when printing an error message if the assertion fails.

assert(boolean[, message])

Passes if the expression boolean has a truthy value.

assertNot(boolean[, message])

Passes if the expression boolean has a falsy value, i.e. is false, 0, '', null or undefined.

assertEqual(expected, actual[, message])

Passes if expected and actual are equal. Equality is determined using the equals() method if expected responds to it, and the === operator otherwise. This assertion also handles deep-equality of arrays and objects, comparing the elements of the object using equals() or === where appropriate.

assertNotEqual(expected, actual[, message])

Passes if expected and actual are not equal according to the semantics of assertEqual().

assertSame(expected, actual[, message])

Passes if expected and actual are the same object, i.e. expected === actual.

assertNotSame(expected, actual[, message])

Passes if expected and actual are not the same object, i.e. expected !== actual.

assertInDelta(expected, actual, delta[, message])

Passes if Math.abs(expected - actual) <= delta. All values must be numbers.

assertKindOf(type, object[, message])

Passes if object is an instance of type. type can be a JavaScript type string like 'boolean' or 'object', or a reference to a class or module.

assertMatch(pattern, string[, message])

Passes if string matches the regular expression pattern. This can also be used with any object that responds to match() as the pattern, for example assertMatch(new JS.Range(1,10), 5).

assertNoMatch(pattern, string[, message])

Passes if pattern does not match string (inverse of assertMatch).

assertNull(object[, message])

Passes if object is null.

assertNotNull(object[, message])

Passes if object is not null.

assertRespondTo(object, method[, message])

Passes if object has a property or method named method.

assertThrows(error[, error2], callback)

Passes if calling the given callback function throws one of the given error types, for example assertThrows(TypeError, function() { null.foo() }).

assertNothingThrown([error, ][message, ]callback)

Passes if calling the given callback does not throw an error, for example assertNothingThrown(function() { 1 + 1 }). You can specify what types of errors should be caught, for example assertNothingThrown(TypeError, ReferenceError, function() { 1 + 1 }); if the callback throws any other type of error this will be reported as an error rather than a test failure.