Exception asserter.
Assert that the actual value is an exception.
The exception() asserter provides adapted assertions for works on an exception.
See also the spec of "exception" asserter for more examples.
Example
var trigger = function(){
throw new Error('Whoops !');
};
test.exception(trigger);
Methods
-
is(expected)
-
Assert
actualexception equality by content and if possible, recursively.Also handles circular and self-referential objects.
For most parts it asserts strict equality (
===), but:Booleanobjects are compared to boolean literals.Numberobjects are compared to number literals.Stringobjects are compared to string literals.RegExpobjects are compared by their pattern and flags.Dateobjects are compared by their value.Arrayobjects are compared recursively.NaNs are considered equivalent.- Instances of the same class with a
valueOffunction 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
ExampleReturnsType Description ObjectThe current instance
var error = new Error('Whoops !'); var trigger = function(){ throw error; }; test .exception(trigger) .is(error) .is(new Error('Whoops !')) ;
See also -
isNot(expected)
-
Assert
actualexception to the negative equality by content and if possible, recursively.Also handles circular and self-referential objects.
For most parts it asserts strict (
!==), but:Booleanobjects are compared to boolean literals.Numberobjects are compared to number literals.Stringobjects are compared to string literals.RegExpobjects are compared by their pattern and flags.Dateobjects are compared by their value.Arrayobjects are compared recursively.NaNs are considered equivalent.- Instances of the same class with a
valueOffunction 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
ExampleReturnsType Description ObjectThe current instance
var error = new Error('Whoops !'); var trigger = function(){ throw error; }; test.exception(trigger).isNot({message: 'Whoops !'});
See also -
isIdenticalTo(expected)
-
Assert that the
actualexception is identical to (===)expectedvalue.ParametersName Type Description expected mixedExpected value
ExampleReturnsType Description ObjectThe current instance
var error = new Error('Whoops !'); var trigger = function(){ throw error; }; test.exception(trigger).isIdenticalTo(error);
See also -
isNotIdenticalTo(expected)
-
Assert that the
actualexception is not identical to (!==)expectedvalue.ParametersName Type Description expected mixedExpected value
ExampleReturnsType Description ObjectThe current instance
var error = new Error('Whoops !'); var trigger = function(){ throw error; }; test.exception(trigger).isNotIdenticalTo(new Error('Whoops !'));
See also -
isEqualTo(expected)
-
Assert that the
actualexception is equal to (==) theexpectedvalue.ParametersName Type Description expected mixedExpected value
ExampleReturnsType Description ObjectThe current instance
var error = new Error('Whoops !'); var trigger = function(){ throw error; }; test.exception(trigger).isEqualTo(error);
See also -
isNotEqualTo(expected)
-
Assert that the
actualexception is not equal to (!=) theexpectedvalue.ParametersName Type Description expected mixedExpected value
ExampleReturnsType Description ObjectThe current instance
var error = new Error('Whoops !'); var trigger = function(){ throw error; }; test.exception(trigger).isNotEqualTo(new Error('Whoops !'));
See also -
match(expected)
-
Assert
actualexception to match theexpectedvalue.ParametersName Type Description expected StringNumberRegExpfunctionExpected matches
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops!'); }) .match('Whoops!') .match(/Whoops/) .match(function(exception){ return (exception instanceof Error) && /whoops/i.test(exception); }) ;
See also -
notMatch(expected)
-
Assert
actualexception to not match theexpectedvalue.ParametersName Type Description expected StringNumberRegExpfunctionExpected value that must not match
ExampleReturnsType Description ObjectThe current instance
test .exception(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
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops!'); }) .isValid('Whoops!') .isValid(/Whoops/) .isValid(function(exception){ return (exception instanceof Error) && /whoops/i.test(exception); }) ;
See also -
isNotValid(expected)
-
Alias of
notMatch().ParametersName Type Description expected StringNumberRegExpfunctionExpected matches
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops!'); }) .isNotValid('Yeah an error') .isNotValid(/Yeah/) .isNotValid(function(exception){ return /yeah/.test(exception); }) ;
See also -
isType(expected)
-
Assert that the type of
actualexception is the givenexpectedvalue (using typeof operator).ParametersName Type Description expected mixedExpected value
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops !'); }) .isType('object') .exception(function(){ throw 'Whoops !'; }) .isType('string') ;
See also -
isNotType(expected)
-
Assert that the type of
actualexception is not the givenexpectedvalue (using typeof operator).ParametersName Type Description expected mixedExpected value
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops !'); }) .isNotType('string') .exception(function(){ throw 'Whoops !'; }) .isNotType('object') ;
See also -
isObject()
-
Assert that the type of
actualexception isobject(using typeof operator).ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops !'); }) .isObject() ;
See also -
isArray()
-
Assert that the type of
actualexception isarray(using typeof operator).ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw ['error']; }) .isArray() ;
See also -
isString()
-
Assert that the type of
actualexception isstring(using typeof operator).ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw 'error'; }) .isString() ;
See also -
isNumber()
-
Assert that the type of
actualexception isnumber(using typeof operator).ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw 0; }) .isNumber() ;
See also -
isBool()
-
Alias of
isBoolean().Assert that the type of
actualexception isboolean(using typeof operator).ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw false; }) .isBool() ;
See also -
isBoolean()
-
Assert that the type of
actualexception isboolean(using typeof operator).ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw false; }) .isBoolean() ;
See also -
isNull()
-
Assert that the type of
actualexception isnull(using typeof operator).ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw null; }) .isNull() ;
See also -
isUndefined()
-
Assert that the type of
actualexception isundefined(using typeof operator).ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw undefined; }) .isUndefined() ;
See also -
isRegExp()
-
Assert that the
actualexception is an instance ofRegExp.ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new RegExp('whoops'); }) .isRegExp() ;
See also -
isNotRegExp()
-
Assert that the
actualexception is not an instance ofRegExp.ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops !'); }) .isNotRegExp() ;
See also -
isDate()
-
Assert that the
actualexception is an instance ofDate.ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Date(); }) .isDate() ;
See also -
isNotDate()
-
Assert that the
actualexception is not an instance ofDate.ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops !'); }) .isNotDate() ;
See also -
isArguments()
-
Assert that the
actualexception is theargumentsobject provided in a function.ExampleReturnsType Description ObjectThe current instance
var error = function(){ return arguments; }; test .exception(function(){ throw error(1, 2, 3); }) .isArguments() ;
See also -
isNotArguments()
-
Assert that the
actualexception is not theargumentsobject provided in a function.ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops !'); }) .isNotArguments() ;
See also -
isEmpty()
-
Assert that the
actualexception is empty.Checks either the
lengthfor arrays or the count of enumerable keys. Inherited keys also counted. Note: an instance ofErrorhas no enumerable properties.ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw ''; }) .isEmpty() .exception(function(){ throw []; }) .isEmpty() .exception(function(){ throw {}; }) .isEmpty() // Indeed, an instance of `Error` has no enumerable properties. .exception(function(){ throw new Error('Whoops !'); }) .isEmpty() ;
See also -
isNotEmpty()
-
Assert that the
actualexception is not empty.Checks either the
lengthfor arrays or the count of enumerable keys. Inherited keys also counted. Note: an instance ofErrorhas no enumerable properties.ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw 'Whoops !'; }) .isNotEmpty() ;
See also -
isError()
-
Assert that the
actualexception is anErrorinstance.ExampleReturnsType Description ObjectThe current instance
// trigger var trigger = function(){ throw new Error("I'm a ninja !") }; test.exception(trigger).isError(); -
hasLength(expected)
-
Assert that the
actualexception has a length property equal toexpectednumber.ParametersName Type Description expected numberExpected length
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .hasLength(2) ;
See also -
hasNotLength(expected)
-
Assert that the
actualexception has not a length property equal toexpectednumber.ParametersName Type Description expected numberExpected length
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .hasNotLength(1) .hasNotLength(3) ;
See also -
isEnumerable(property)
-
Assert that the
actualexception has an enumerableproperty. It will fail if theactualvalue lacks thepropertyentirely.This also checks inherited properties in the prototype chain, something which
Object.prototype.propertyIsEnumerableitself does not do.For checking if a property exists and is non-enumerable, see
isNotEnumerable().ParametersName Type Description property StringThe property name
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .isEnumerable('message') .isEnumerable('code') ;
See also -
isNotEnumerable(property)
-
Assert that the
actualexception has a non-enumerableproperty. It will fail if theactualvalue lacks thepropertyentirely.This also checks inherited properties in the prototype chain, something which
Object.prototype.propertyIsEnumerableitself does not do.It's the inverse of
isEnumerable().ParametersName Type Description property StringThe property name
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops !'); }) .isNotEnumerable('message') ;
See also -
isFrozen()
-
Assert that the
actualexception is frozen withObject.isFrozen.ExampleReturnsType Description ObjectThe current instance
var frozenError = {message: 'error', code: 42}; Object.freeze(frozenError); test .exception(function(){ throw frozenError; }) .isFrozen() ;
See also -
isNotFrozen()
-
Assert that the
actualexception is not frozen withObject.isFrozen.ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops !'); }) .isNotFrozen() ;
See also -
isInstanceOf(expected)
-
Assert that the
actualexception is an instance ofexpectedvalue.Uses
actual instanceof expected.ParametersName Type Description expected ObjectExpected instance
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new TypeError('Whoops !'); }) .isInstanceOf(TypeError) ;
See also -
isNotInstanceOf(expected)
-
Assert that the
actualexception is not an instance ofexpectedvalue.Uses
actual instanceof expected === false.ParametersName Type Description expected ObjectExpected instance
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops !'); }) .isNotInstanceOf(TypeError) ;
See also -
hasProperty(property, value)
-
Assert that the
actualtested exception hasproperty. Optionally assert it equals(===)tovalueargument.Takes inherited properties into account. To not do so, see
hasOwnProperty().ParametersName Type Argument Description property StringThe property name
value mixedoptional The property value
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .hasProperty('message') .hasProperty('code', 42) .exception(function(){ throw new Error('Whoops !'); }) .hasProperty('message') .hasProperty('message', 'Whoops !') .hasProperty('constructor') ;
See also -
hasNotProperty(property, value)
-
Assert that the
actualtested exception has not aproperty. Optionally assert it not equals(!==)tovalueargument.Takes inherited properties into account. To not do so, see
hasNotOwnProperty().ParametersName Type Argument Description property StringThe property name
value mixedoptional The property value
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .hasNotProperty('foo') .hasNotProperty('code', 1) ;
See also -
hasOwnProperty(property, value)
-
Assert that the
actualtested exception has ownproperty. Optionally assert it equals(===)tovalueargument.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
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops !'); }) .hasOwnProperty('message') .hasOwnProperty('message', 'Whoops !') ;
See also -
hasNotOwnProperty(property, value)
-
Assert that the
actualtested exception has not ownproperty. Optionally assert it not equals(!==)tovalueargument.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
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops !'); }) .hasNotOwnProperty('foo') .hasNotOwnProperty('message', 'Grrrr !') .hasNotOwnProperty('constructor') ;
See also -
hasProperties(properties)
-
Assert that the
actualexception has only the expected enumerableproperties. Pass an array of strings asproperties.Takes inherited properties into account. To not do so, see
hasOwnProperties().ParametersName Type Description properties ArrayAn array of expected properties
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .hasProperties(['message', 'code']) ;
See also -
hasNotProperties(properties)
-
Assert that the
actualexception has not the enumerableproperties. Pass an array of strings asproperties.Takes inherited properties into account. To not do so, see
hasNotOwnProperties().ParametersName Type Description properties ArrayAn array of properties
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .hasNotProperties(['foo', 'bar']) .hasNotProperties(['foo', 'code', 'bar']) ;
See also -
hasOwnProperties(properties)
-
Assert that the
actualexception has only the expected enumerablepropertiesof its own. Pass an array of strings asproperties.Does not take inherited properties into account. To do so, see
hasProperties.ParametersName Type Description properties ArrayAn array of expected properties
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .hasOwnProperties(['message', 'code']) ;
See also -
hasKey(key, value)
-
Alias of hasProperty()Assert that the
actualtested exception has akey. Optionally assert it equals(===)tovalueargument.Takes inherited properties into account. To not do so, see
hasOwnProperty().ParametersName Type Argument Description key StringThe key name
value mixedoptional The key value
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .hasKey('message') .hasKey('code', 42) .exception(function(){ throw new Error('Whoops !'); }) .hasKey('message') .hasKey('message', 'Whoops !') .hasKey('constructor') ;
See also -
notHasKey(key, value)
-
Alias of hasNotProperty()Assert that the
actualtested exception has not akey. Optionally assert it not equals(!==)tovalueargument.Takes inherited properties into account. To not do so, see
hasNotOwnProperty().ParametersName Type Argument Description key StringThe key name
value mixedoptional The key value
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .notHasKey('foo') .notHasKey('code', 1) ;
See also -
hasKeys(keys)
-
Alias of hasProperties()Assert that the
actualexception has only the expected enumerablekeys. Pass an array of strings askeys.Takes inherited properties into account. To not do so, see
hasOwnProperties().ParametersName Type Description keys ArrayAn array of expected keys
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .hasKeys(['message', 'code']) ;
See also -
notHasKeys(keys)
-
Alias of hasNotProperties()Assert that the
actualexception has not the enumerablekeys. Pass an array of strings askeys.Takes inherited properties into account. To not do so, see
hasNotOwnProperties().ParametersName Type Description keys ArrayAn array of keys
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .notHasKeys(['foo', 'bar']) .notHasKeys(['foo', 'code', 'bar']) ;
See also -
hasValue(expected)
-
Assert that the
actualtested exception hasexpectedvalue.For strings it checks the text, for arrays it checks elements and for objects the property values. Everything is checked with strict equals
(===).ParametersName Type Description expected mixedThe expected value
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .hasValue('error') .hasValue(42) ;
See also -
notHasValue(expected)
-
Assert that the
actualtested exception not hasexpectedvalue.For strings it checks the text, for arrays it checks elements and for objects the property values. Everything is checked with strict equals
(!==).ParametersName Type Description expected mixedThe expected value
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .notHasValue('err') .notHasValue(2) ;
See also -
hasValues(expected)
-
Assert that the
actualtested exception has severalexpectedvalues passed in an array of expected values.ParametersName Type Description expected ArrayAn array of expected values
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .hasValues(['error']) .hasValues(['error', 42]) ;
See also -
notHasValues(expected)
-
Assert that the
actualtested exception not has severalexpectedvalues passed in an array of expected values.ParametersName Type Description expected ArrayAn array of expected values
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw {message: 'error', code: 42}; }) .notHasValues(['code']) .notHasValues(['message', 'code', 'foo']) ;
See also -
contains(expected)
-
Assert that the
actualexception to contain something(===)toexpectedwithin depth.ParametersName Type Description expected Array[, other_excepted_values] The expected value
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops'); }) .contains({message: 'Whoops'}) ;
See also -
notContains(expected)
-
Assert that the
actualexception to not contain something(!==)toexpectedwithin depth.ParametersName Type Description expected Array[, other_excepted_values] The expected value
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops'); }) .notContains({message: 'foo'}) ;
See also -
startsWith(str)
-
Assert that the
actualexception (string) starts withstr.ParametersName Type Description str StringExpected
stringvalueExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw 'An error occured'; }) .startsWith('An error') ;
See also -
notStartsWith(str)
-
Assert that the
actualexception (string) not starts withstr.ParametersName Type Description str StringExpected
stringvalueExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw 'An error occured'; }) .notStartsWith('error') ;
See also -
endsWith(str)
-
Assert that the
actualexception (string) ends withstr.ParametersName Type Description str StringExpected
stringvalueExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw 'An error occured'; }) .endsWith('occured') ;
See also -
notEndsWith(str)
-
Assert that the
actualexception (string) not ends withstr.ParametersName Type Description str StringExpected
stringvalueExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw 'An error occured'; }) .notEndsWith('error') ;
See also -
hasMessage(expected)
-
Alias of
match().Assert that the
actualexception has anexpectedmessage.ParametersName Type Description expected StringRegExpExpected message
ExampleReturnsType Description ObjectThe current instance
test .exception(function(){ throw new Error('Whoops!'); }) .hasMessage('Whoops!') .hasMessage(/Whoops/) ;
See also