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.NaN
s 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.
ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
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.NaN
s 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.
ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
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.ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
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.ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
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 (==
) theexpected
value.ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
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 (!=
) theexpected
value.ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
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 theexpected
value.ParametersName Type Description expected StringNumberRegExpfunctionExpected matches
ReturnsType Description ObjectThe current instance
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 theexpected
value.ParametersName Type Description expected StringNumberRegExpfunctionExpected value that must not match
ReturnsType Description ObjectThe current instance
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()
.ParametersName Type Description expected StringNumberRegExpfunctionExpected matches
ReturnsType Description ObjectThe current instance
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()
.ParametersName Type Description expected StringNumberRegExpfunctionExpected matches
ReturnsType Description ObjectThe current instance
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 enumerableproperty
. It will fail if theactual
value lacks theproperty
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()
.ParametersName Type Description property StringThe property name
ReturnsType Description ObjectThe current instance
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-enumerableproperty
. It will fail if theactual
value lacks theproperty
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()
.ParametersName Type Description property StringThe property name
ReturnsType Description ObjectThe current instance
test .error(function(){ throw new Error('Whoops !'); }) .isNotEnumerable('message') ;
See also -
isFrozen()
-
Assert that the
actual
Error
exception is frozen withObject.isFrozen
.ReturnsType Description ObjectThe current instance
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 withObject.isFrozen
.ReturnsType Description ObjectThe current instance
test .error(function(){ throw new Error('Whoops !'); }) .isNotFrozen() ;
See also -
isInstanceOf(expected)
-
Assert that the
actual
Error
exception is an instance ofexpected
value.Uses
actual instanceof expected
.ParametersName Type Description expected ObjectExpected instance
ReturnsType Description ObjectThe current instance
test .error(function(){ throw new TypeError('Whoops !'); }) .isInstanceOf(TypeError) ;
See also -
isNotInstanceOf(expected)
-
Assert that the
actual
Error
exception is not an instance ofexpected
value.Uses
actual instanceof expected === false
.ParametersName Type Description expected ObjectExpected instance
ReturnsType Description ObjectThe current instance
test .error(function(){ throw new Error('Whoops !'); }) .isNotInstanceOf(TypeError) ;
See also -
hasProperty(property, value)
-
Assert that the
actual
testedError
exception hasproperty
. Optionally assert it equals(===)
tovalue
argument.Takes inherited properties into account. To not do so, see
hasOwnProperty()
.ParametersName Type Argument Description property StringThe property name
value mixedoptional The property value
ReturnsType Description ObjectThe current instance
test .error(function(){ throw new Error('Whoops !'); }) .hasProperty('message') .hasProperty('message', 'Whoops !') .hasProperty('constructor') ;
See also -
hasNotProperty(property, value)
-
Assert that the
actual
testedError
exception has not aproperty
. Optionally assert it not equals(!==)
tovalue
argument.Takes inherited properties into account. To not do so, see
hasNotOwnProperty()
.ParametersName Type Argument Description property StringThe property name
value mixedoptional The property value
ReturnsType Description ObjectThe current instance
test .error(function(){ throw new Error('Whoops !'); }) .hasNotProperty('foo') .hasNotProperty('message', 'whoops') ;
See also -
hasOwnProperty(property, value)
-
Assert that the
actual
testedError
exception has ownproperty
. Optionally assert it equals(===)
tovalue
argument.Does not take inherited properties into account. To do so, see
hasProperty()
.ParametersName Type Argument Description property StringThe property name
value mixedoptional The property value
ReturnsType Description ObjectThe current instance
test .error(function(){ throw new Error('Whoops !'); }) .hasOwnProperty('message') .hasOwnProperty('message', 'Whoops !') ;
See also -
hasNotOwnProperty(property, value)
-
Assert that the
actual
testedError
exception has not ownproperty
. Optionally assert it not equals(!==)
tovalue
argument.Does not take inherited properties into account. To do so, see
hasNotProperty()
.ParametersName Type Argument Description property StringThe property name
value mixedoptional The property value
ReturnsType Description ObjectThe current instance
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
testedError
exception has akey
. Optionally assert it equals(===)
tovalue
argument.Takes inherited properties into account. To not do so, see
hasOwnProperty()
.ParametersName Type Argument Description key StringThe key name
value mixedoptional The key value
ReturnsType Description ObjectThe current instance
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
testedError
exception has not akey
. Optionally assert it not equals(!==)
tovalue
argument.Takes inherited properties into account. To not do so, see
hasNotOwnProperty()
.ParametersName Type Argument Description key StringThe key name
value mixedoptional The key value
ReturnsType Description ObjectThe current instance
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 anexpected
message.ParametersName Type Description expected StringRegExpExpected message
ReturnsType Description ObjectThe current instance
test .error(function(){ throw new Error('Whoops!'); }) .hasMessage('Whoops!') .hasMessage(/Whoops/) ;
See also