Function asserter.
Assert that the actual
value is a function
(using .isFunction()
assertion).
The function()
asserter provides adapted assertions for works on a function
.
See also the spec of "function" asserter for more examples.
Example
test.function(actual);
Methods
-
is(expected)
-
Assert
actual
function equality by content and if possible, recursively.Also handles circular and self-referential objects.
For most parts it asserts strict equality (
===
), but:- Instances of the same class with a
valueOf
function are compared by its output. - Plain objects and instances of the same class are compared recursively.
Does not coerce types so mismatching types fail. Inherited enumerable properties are also taken into account.
ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
var fn = function(){}; var ref = fn; test.function(ref).is(fn);
See also - Instances of the same class with a
-
isNot(expected)
-
Assert
actual
function to the negative equality by content and if possible, recursively.Also handles circular and self-referential objects.
For most parts it asserts strict (
!==
), but:- Instances of the same class with a
valueOf
function are compared by its output. - Plain objects and instances of the same class are compared recursively.
Does not coerce types so mismatching types fail. Inherited enumerable properties are also taken into account.
ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
var fn = function(){}; var otherFunction = function(){}; test.function(fn).isNot(otherFunction);
See also - Instances of the same class with a
-
isIdenticalTo(expected)
-
Assert that the
actual
function is identical to (===
)expected
function.ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
var fn = function(){}; var ref = fn; test.function(ref).isIdenticalTo(fn);
See also -
isNotIdenticalTo(expected)
-
Assert that the
actual
function is not identical to (!==
)expected
function.ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
var fn = function(){}; var otherFunction = function(){}; test.function(fn).isNotIdenticalTo(otherFunction);
See also -
isEqualTo(expected)
-
Assert that the
actual
function is equal to (==
) theexpected
function.ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
var fn = function(){}; var ref = fn; test.function(ref).isEqualTo(fn);
See also -
isNotEqualTo(expected)
-
Assert that the
actual
function is not equal to (!=
) theexpected
function.ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
var fn = function(){}; var otherFunction = function(){}; test.function(fn).isNotEqualTo(otherFunction);
See also -
match(expected)
-
Assert
actual
function to match theexpected
function.ParametersName Type Description expected StringNumberRegExpfunctionExpected matches
ReturnsType Description ObjectThe current instance
var fn = function(){ return 'hello'; }; test.function(fn).match(function(it){ return it() === 'hello'; });
See also -
notMatch(expected)
-
Assert
actual
function to not match theexpected
function.ParametersName Type Description expected StringNumberRegExpfunctionExpected value that must not match
ReturnsType Description ObjectThe current instance
var fn = function(){ return 'hello'; }; test.function(fn).notMatch(function(it){ return it() === 'bye'; });
See also -
isValid(expected)
-
Alias of
match()
.ParametersName Type Description expected StringNumberRegExpfunctionExpected matches
ReturnsType Description ObjectThe current instance
var fn = function(){ return 'hello'; }; test.function(fn).isValid(function(it){ return it() === 'hello'; });
See also -
isNotValid(expected)
-
Alias of
notMatch()
.ParametersName Type Description expected StringNumberRegExpfunctionExpected matches
ReturnsType Description ObjectThe current instance
var fn = function(){ return 'hello'; }; test.function(fn).isNotValid(function(it){ return it() === 'bye'; });
See also -
throws(constructor, expected)
-
Assert that the
actual
function (trigger function) throws.Optionally assert it throws expected (of possibly instance
constructor
).Given expected, the error is asserted as follows:
- A string is compared with the exception's
message
property. - A regular expression is matched against the exception's
message
property. - A function (a.k.a. constructor) is used to check if the error is an
instanceof
that constructor. - All other cases of
expected
are left unspecified for now.
Because of how JavaScript works, the function will be called in
null
context (this
). If you want to test an instance method, bind it:test.function(obj.method.bind(obj)).throws()
.ParametersName Type Argument Description constructor StringRegExpfunctionconstructoroptional expected optional ReturnsType Description ObjectThe current instance
// trigger var trigger = function(){ throw new Error("I'm a ninja !") }; test.function(trigger) .throws() .throws("I'm a ninja !") .throws(/ninja/) .throws(Error) .throws(Error, "I'm a ninja !") .throws(Error, /ninja/) .throws(function(err) { if ((err instanceof Error) && /ninja/.test(err) ) { return true; } });
See also - A string is compared with the exception's
-
isError()
-
Assert that the
actual
function (trigger function) throws an instance ofError
(or inherited).ReturnsType Description ObjectThe current instance
// trigger var trigger = function(){ throw new Error("I'm a ninja !") }; test.function(trigger).isError();
See also -
hasName(expected)
-
Assert that the
actual
function has anexpected
name.ParametersName Type Description expected StringExpected name
ReturnsType Description ObjectThe current instance
test.function(Date).hasName('Date');