function

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.

Parameters
Name Type Description
expected
mixed

Expected value

Returns
Type Description
Object

The current instance

Example
var fn = function(){};
var ref = fn;

test.function(ref).is(fn);
  

See also

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.

Parameters
Name Type Description
expected
mixed

Expected value

Returns
Type Description
Object

The current instance

Example
var fn = function(){};
var otherFunction = function(){};

test.function(fn).isNot(otherFunction);

See also

isIdenticalTo(expected)


Assert that the actual function is identical to (===) expected function.

Parameters
Name Type Description
expected
mixed

Expected value

Returns
Type Description
Object

The current instance

Example
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.

Parameters
Name Type Description
expected
mixed

Expected value

Returns
Type Description
Object

The current instance

Example
var fn = function(){};
var otherFunction = function(){};

test.function(fn).isNotIdenticalTo(otherFunction);

See also

isEqualTo(expected)


Assert that the actual function is equal to (==) the expected function.

Parameters
Name Type Description
expected
mixed

Expected value

Returns
Type Description
Object

The current instance

Example
var fn = function(){};
var ref = fn;

test.function(ref).isEqualTo(fn);

See also

isNotEqualTo(expected)


Assert that the actual function is not equal to (!=) the expected function.

Parameters
Name Type Description
expected
mixed

Expected value

Returns
Type Description
Object

The current instance

Example
var fn = function(){};
var otherFunction = function(){};

test.function(fn).isNotEqualTo(otherFunction);

See also

match(expected)


Assert actual function to match the expected function.

Parameters
Name Type Description
expected
String
Number
RegExp
function

Expected matches

Returns
Type Description
Object

The current instance

Example
var fn = function(){
  return 'hello';
};

test.function(fn).match(function(it){
  return it() === 'hello';
});

See also

notMatch(expected)


Assert actual function to not match the expected function.

Parameters
Name Type Description
expected
String
Number
RegExp
function

Expected value that must not match

Returns
Type Description
Object

The current instance

Example
var fn = function(){
  return 'hello';
};

test.function(fn).notMatch(function(it){
  return it() === 'bye';
});

See also

isValid(expected)


Alias of match().

Parameters
Name Type Description
expected
String
Number
RegExp
function

Expected matches

Returns
Type Description
Object

The current instance

Example
var fn = function(){
  return 'hello';
};

test.function(fn).isValid(function(it){
  return it() === 'hello';
});

See also

isNotValid(expected)


Alias of notMatch().

Parameters
Name Type Description
expected
String
Number
RegExp
function

Expected matches

Returns
Type Description
Object

The current instance

Example
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().

Parameters
Name Type Argument Description
constructor
String
RegExp
function
constructor
optional 
expected optional 
Returns
Type Description
Object

The current instance

Example
// 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

isError()


Assert that the actual function (trigger function) throws an instance of Error (or inherited).

Returns
Type Description
Object

The current instance

Example
// 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 an expected name.

Parameters
Name Type Description
expected
String

Expected name

Returns
Type Description
Object

The current instance

Example
test.function(Date).hasName('Date');