error

Exception asserter.

Assert that the actual value is an exception and is an instance of Error (or inherited). The error() asserter provides adapted assertions for works on an Error exception.

See also the spec of "error" asserter for more examples.

Example

var trigger = function(){
  throw new Error('Whoops !');
};

test.error(trigger);

Methods

is(expected)


Assert actual Error exception equality by content and if possible, recursively.

Also handles circular and self-referential objects.

For most parts it asserts strict equality (===), but:

  • Boolean objects are compared to boolean literals.
  • Number objects are compared to number literals.
  • String objects are compared to string literals.
  • RegExp objects are compared by their pattern and flags.
  • Date objects are compared by their value.
  • Array objects are compared recursively.
  • NaNs are considered equivalent.
  • 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 error = new Error('Whoops !');
var trigger = function(){ 
  throw error; 
}; 
 
test 
  .error(trigger)
    .is(error)
    .is(new Error('Whoops !'))
;
 

See also

isNot(expected)


Assert actual Error exception to the negative equality by content and if possible, recursively.

Also handles circular and self-referential objects.

For most parts it asserts strict (!==), but:

  • Boolean objects are compared to boolean literals.
  • Number objects are compared to number literals.
  • String objects are compared to string literals.
  • RegExp objects are compared by their pattern and flags.
  • Date objects are compared by their value.
  • Array objects are compared recursively.
  • NaNs are considered equivalent.
  • 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 error = new Error('Whoops !');
var trigger = function(){ 
  throw error; 
}; 

test.error(trigger).isNot({message: 'Whoops !'});
 

See also

isIdenticalTo(expected)


Assert that the actual Error exception is identical to (===) expected value.

Parameters
Name Type Description
expected
mixed

Expected value

Returns
Type Description
Object

The current instance

Example
var error = new Error('Whoops !'); 
var trigger = function(){
 throw error;
}; 

test.error(trigger).isIdenticalTo(error);

See also

isNotIdenticalTo(expected)


Assert that the actual Error exception is not identical to (!==) expected value.

Parameters
Name Type Description
expected
mixed

Expected value

Returns
Type Description
Object

The current instance

Example
var error = new Error('Whoops !'); 
var trigger = function(){
 throw error;
}; 

test.error(trigger).isNotIdenticalTo(new Error('Whoops !'));

See also

isEqualTo(expected)


Assert that the actual Error exception is equal to (==) the expected value.

Parameters
Name Type Description
expected
mixed

Expected value

Returns
Type Description
Object

The current instance

Example
var error = new Error('Whoops !'); 
var trigger = function(){
 throw error;
}; 

test.error(trigger).isEqualTo(error);

See also

isNotEqualTo(expected)


Assert that the actual Error exception is not equal to (!=) the expected value.

Parameters
Name Type Description
expected
mixed

Expected value

Returns
Type Description
Object

The current instance

Example

var error = new Error('Whoops !'); 
var trigger = function(){
 throw error;
}; 

test.error(trigger).isNotEqualTo(new Error('Whoops !'));

See also

match(expected)


Assert actual Error exception to match the expected value.

Parameters
Name Type Description
expected
String
Number
RegExp
function

Expected matches

Returns
Type Description
Object

The current instance

Example
test
  .error(function(){
    throw new Error('Whoops!');
  })
  .match('Whoops!')
  .match(/Whoops/)
  .match(function(exception){ 
    return /whoops/i.test(exception); 
  })
;

See also

notMatch(expected)


Assert actual Error exception to not match the expected value.

Parameters
Name Type Description
expected
String
Number
RegExp
function

Expected value that must not match

Returns
Type Description
Object

The current instance

Example
test
  .error(function(){
    throw new Error('Whoops!');
  })
  .notMatch('Yeah an error')
  .notMatch(/Yeah/)
  .notMatch(function(exception){ 
    return /yeah/.test(exception);
  })
;
 

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
test
 .error(function(){
   throw new Error('Whoops!');
 })
 .isValid('Whoops!')
 .isValid(/Whoops/)
 .isValid(function(exception){ 
   return /whoops/i.test(exception); 
 })
;

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
test
  .error(function(){
    throw new Error('Whoops!');
  })
  .isNotValid('Yeah an error')
  .isNotValid(/Yeah/)
  .isNotValid(function(exception){ 
    return /yeah/.test(exception);
  })
;

See also

isEnumerable(property)


Assert that the actual Error exception has an enumerable property. It will fail if the actual value lacks the property entirely.

This also checks inherited properties in the prototype chain, something which Object.prototype.propertyIsEnumerable itself does not do.

For checking if a property exists and is non-enumerable, see isNotEnumerable().

Parameters
Name Type Description
property
String

The property name

Returns
Type Description
Object

The current instance

Example
var error = new Error('Whoops !');
error.foo = 'bar';

test
  .error(function(){
    throw error;
  })
  .isEnumerable('foo')
;

See also

isNotEnumerable(property)


Assert that the actual Error exception has a non-enumerable property. It will fail if the actual value lacks the property entirely.

This also checks inherited properties in the prototype chain, something which Object.prototype.propertyIsEnumerable itself does not do.

It's the inverse of isEnumerable().

Parameters
Name Type Description
property
String

The property name

Returns
Type Description
Object

The current instance

Example
test
  .error(function(){
    throw new Error('Whoops !');
  })
  .isNotEnumerable('message')
;

See also

isFrozen()


Assert that the actual Error exception is frozen with Object.isFrozen.

Returns
Type Description
Object

The current instance

Example
var frozenError = new Error('Whoops !');

Object.freeze(frozenError);

test
  .error(function(){
    throw frozenError;
  })
  .isFrozen()
;

See also

isNotFrozen()


Assert that the actual Error exception is not frozen with Object.isFrozen.

Returns
Type Description
Object

The current instance

Example

test
  .error(function(){
    throw new Error('Whoops !');
  })
  .isNotFrozen()
;

See also

isInstanceOf(expected)


Assert that the actual Error exception is an instance of expected value.

Uses actual instanceof expected.

Parameters
Name Type Description
expected
Object

Expected instance

Returns
Type Description
Object

The current instance

Example
test
  .error(function(){
    throw new TypeError('Whoops !');
  })
  .isInstanceOf(TypeError)
;

See also

isNotInstanceOf(expected)


Assert that the actual Error exception is not an instance of expected value.

Uses actual instanceof expected === false.

Parameters
Name Type Description
expected
Object

Expected instance

Returns
Type Description
Object

The current instance

Example
test
  .error(function(){
    throw new Error('Whoops !');
  })
  .isNotInstanceOf(TypeError)
;
 

See also

hasProperty(property, value)


Assert that the actual tested Error exception has property. Optionally assert it equals (===) to value argument.

Takes inherited properties into account. To not do so, see hasOwnProperty().

Parameters
Name Type Argument Description
property
String

The property name

value
mixed
optional 

The property value

Returns
Type Description
Object

The current instance

Example
test
  .error(function(){
    throw new Error('Whoops !');
  })
  .hasProperty('message')
  .hasProperty('message', 'Whoops !')
  .hasProperty('constructor')
; 

See also

hasNotProperty(property, value)


Assert that the actual tested Error exception has not a property. Optionally assert it not equals (!==) to value argument.

Takes inherited properties into account. To not do so, see hasNotOwnProperty().

Parameters
Name Type Argument Description
property
String

The property name

value
mixed
optional 

The property value

Returns
Type Description
Object

The current instance

Example
test
  .error(function(){
    throw new Error('Whoops !');
  })
  .hasNotProperty('foo')
  .hasNotProperty('message', 'whoops')
;

See also

hasOwnProperty(property, value)


Assert that the actual tested Error exception has own property. Optionally assert it equals (===) to value argument.

Does not take inherited properties into account. To do so, see hasProperty().

Parameters
Name Type Argument Description
property
String

The property name

value
mixed
optional 

The property value

Returns
Type Description
Object

The current instance

Example
test
  .error(function(){
    throw new Error('Whoops !');
  }) 
  .hasOwnProperty('message')
  .hasOwnProperty('message', 'Whoops !') 
;

See also

hasNotOwnProperty(property, value)


Assert that the actual tested Error exception has not own property. Optionally assert it not equals (!==) to value argument.

Does not take inherited properties into account. To do so, see hasNotProperty().

Parameters
Name Type Argument Description
property
String

The property name

value
mixed
optional 

The property value

Returns
Type Description
Object

The current instance

Example
test
  .error(function(){
    throw new Error('Whoops !');
  })
  .hasNotOwnProperty('foo')
  .hasNotOwnProperty('message', 'Grrrr !')
  .hasNotOwnProperty('constructor') 
;

See also

hasKey(key, value)


Alias of hasProperty()

Assert that the actual tested Error exception has a key. Optionally assert it equals (===) to value argument.

Takes inherited properties into account. To not do so, see hasOwnProperty().

Parameters
Name Type Argument Description
key
String

The key name

value
mixed
optional 

The key value

Returns
Type Description
Object

The current instance

Example
test
  .error(function(){
    throw new Error('Whoops !');
  })
  .hasKey('message')
  .hasKey('message', 'Whoops !')
  .hasKey('constructor')
;

See also

notHasKey(key, value)


Alias of hasNotProperty()

Assert that the actual tested Error exception has not a key. Optionally assert it not equals (!==) to value argument.

Takes inherited properties into account. To not do so, see hasNotOwnProperty().

Parameters
Name Type Argument Description
key
String

The key name

value
mixed
optional 

The key value

Returns
Type Description
Object

The current instance

Example
test
  .error(function(){
    throw new Error('Whoops !');
  })
  .notHasKey('foo')
  .notHasKey('message', 'whoops')
;

See also

hasMessage(expected)


Alias of match().

Assert that the actual Error exception has an expected message.

Parameters
Name Type Description
expected
String
RegExp

Expected message

Returns
Type Description
Object

The current instance

Example
test
  .error(function(){
    throw new Error('Whoops!');
  })
  .hasMessage('Whoops!')
  .hasMessage(/Whoops/)
;

See also