Object asserter.
Assert that the actual
value is an object
(using .isObject()
assertion).
The object()
asserter provides adapted assertions for works on an object
.
See also the spec of "object" asserter for more examples.
Example
test.object(actual);
Methods
-
is(expected)
-
Assert
actual
object 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
test.object({fluent: 'is awesome', deep: [0, 1]}) .is({fluent: 'is awesome', deep: [0, 1]});
See also -
isNot(expected)
-
Assert
actual
object 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
test.object({fluent: 'is awesome', deep: [0, 1]}) .isNot({fluent: 'is awesome', deep: [0, '1']});
See also -
isIdenticalTo(expected)
-
Assert that the
actual
object is identical to (===
)expected
value.ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
var obj = {}, obj2 = obj ; test.object(obj).isIdenticalTo(obj2);
See also -
isNotIdenticalTo(expected)
-
Assert that the
actual
object is not identical to (!==
)expected
value.ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
var obj = {}; test.object(obj).isNotIdenticalTo({});
See also -
isEqualTo(expected)
-
Assert that the
actual
object is equal to (==
) theexpected
value.ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
var obj = {}, obj2 = obj ; test.object(obj).isEqualTo(obj2);
See also -
isNotEqualTo(expected)
-
Assert that the
actual
object is not equal to (!=
) theexpected
value.ParametersName Type Description expected mixedExpected value
ReturnsType Description ObjectThe current instance
var obj = {foo: 'bar'}; test.object(obj).isNotEqualTo({foo: 'bar', baz: 'bar'});
See also -
match(expected)
-
Assert
actual
object to match theexpected
value.ParametersName Type Description expected StringNumberRegExpfunctionExpected matches
ReturnsType Description ObjectThe current instance
test.object({hello: 'world'}).match(function(obj){ return obj.hello == 'world'; });
See also -
notMatch(expected)
-
Assert
actual
object to not match theexpected
value.ParametersName Type Description expected StringNumberRegExpfunctionExpected value that must not match
ReturnsType Description ObjectThe current instance
test.object({hello: 'world'}).notMatch(function(obj){ return obj.hello == 'E.T'; });
See also -
isValid(expected)
-
Alias of
match()
.ParametersName Type Description expected StringNumberRegExpfunctionExpected matches
ReturnsType Description ObjectThe current instance
test.object({hello: 'world'}).isValid(function(obj){ return obj.hello == 'world'; });
See also -
isNotValid(expected)
-
Alias of
notMatch()
.ParametersName Type Description expected StringNumberRegExpfunctionExpected matches
ReturnsType Description ObjectThe current instance
test.object({hello: 'world'}).isNotValid(function(obj){ return obj.hello == 'E.T'; });
See also -
matchEach(expected)
-
Assert
actual
object to match eachexpected
value.ParametersName Type Description expected ArrayStringNumberRegExpfunctionExpected matches for each
ReturnsType Description ObjectThe current instance
test .object({foo: 'bar', hey: 'you', joker:1}) .matchEach(function(it, key) { if(key == 'joker'){ return (typeof it == 'number'); } return (typeof it == 'string'); }) .exception(function(){ // error if one or several does not match test.object({foo: 'bar', hey: 'you', joker:1}).matchEach(function(it, key) { return (typeof it == 'string'); }); }) ;
See also -
notMatchEach(expected)
-
Assert
actual
object to not match one or severalexpected
value.ParametersName Type Description expected ArrayStringNumberRegExpfunctionExpected value that must not match
ReturnsType Description ObjectThe current instance
test .object({foo: 'bar', hey: 'you', joker:1}).notMatchEach(function(it, key) { if(key == 'other'){ return true; } }) .exception(function(){ // error if one or several does not match test.object({foo: 'bar', hey: 'you', joker:1}).notMatchEach(function(it, key) { if(key == 'foo'){ return true; } }); }) .exception(function(){ // error if one or several does not match test.object({foo: 'bar', hey: 'you', joker:1}).notMatchEach(function(it, key) { return (typeof it == 'string'); }); }) ;
See also -
isArray()
-
Assert that
actual
object is anarray
(using typeof operator).ReturnsType Description ObjectThe current instance
test.object([]).isArray();
See also -
isRegExp()
-
Assert that the
actual
object is an instance ofRegExp
.ReturnsType Description ObjectThe current instance
test .object(/[0-9]+/) .isRegExp() .object(new RegExp('ab+c', 'i')) .isRegExp() ;
See also -
isNotRegExp()
-
Assert that the
actual
object is not an instance ofRegExp
.ReturnsType Description ObjectThe current instance
test.object(new Date()).isNotRegExp();
See also -
isDate()
-
Assert that the
actual
object is an instance ofDate
.ReturnsType Description ObjectThe current instance
test.object(new Date()).isDate();
See also -
isNotDate()
-
Assert that the
actual
object is not an instance ofDate
.ReturnsType Description ObjectThe current instance
test.object(/[0-9]+/).isNotDate();
See also -
isArguments()
-
Assert that the
actual
object is thearguments
object provided in a function.ReturnsType Description ObjectThe current instance
var fn = function(){ var args = arguments; test.object(arguments).isArguments(); test.object(args).isArguments(); }; fn(1, 2, 3);
See also -
isNotArguments()
-
Assert that the
actual
object is not thearguments
object provided in a function.ReturnsType Description ObjectThe current instance
var fn = function(){ test .object(arguments) .isArguments() .object([1, 2, 3]) .isNotArguments() .object({0:1, 1:2, 2:3}) .isNotArguments() ; }; fn(1, 2, 3);
See also -
isEmpty()
-
Assert that the
actual
object is empty.Checks either the
length
for arrays or the count of enumerable keys. Inherited keys also counted.ReturnsType Description ObjectThe current instance
test.object({}).isEmpty();
See also -
isNotEmpty()
-
Assert that the
actual
object is not empty.Checks either the
length
for arrays or the count of enumerable keys. Inherited keys also counted.ReturnsType Description ObjectThe current instance
test.object({hello: 'Nico'}).isNotEmpty();
See also -
hasLength(expected)
-
Assert that the
actual
object has a length property equal toexpected
number.ParametersName Type Description expected numberExpected length
ReturnsType Description ObjectThe current instance
test.object({foo: 'bar', other: 'baz'}).hasLength(2);
See also -
hasNotLength(expected)
-
Assert that the
actual
object has not a length property equal toexpected
number.ParametersName Type Description expected numberExpected length
ReturnsType Description ObjectThe current instance
test.object({foo: 'bar', other: 'baz'}).hasNotLength(4);
See also -
isEnumerable(property)
-
Assert that the
actual
object 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
test.object({prop: 'foobar'}).isEnumerable('prop');
See also -
isNotEnumerable(property)
-
Assert that the
actual
object 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 .object(Object.create({}, {prop: {enumerable: 0}})) .isNotEnumerable('prop') ;
See also -
isFrozen()
-
Assert that the
actual
object is frozen withObject.isFrozen
.ReturnsType Description ObjectThe current instance
test.object(Object.freeze({})).isFrozen();
See also -
isNotFrozen()
-
Assert that the
actual
object is not frozen withObject.isFrozen
.ReturnsType Description ObjectThe current instance
test.object({}).isNotFrozen();
See also -
isInstanceOf(expected)
-
Assert that the
actual
object is an instance ofexpected
value.Uses
actual instanceof expected
.ParametersName Type Description expected ObjectExpected instance
ReturnsType Description ObjectThe current instance
test.object(new Date()).isInstanceOf(Date);
See also -
isNotInstanceOf(expected)
-
Assert that the
actual
object is not an instance ofexpected
value.Uses
actual instanceof expected === false
.ParametersName Type Description expected ObjectExpected instance
ReturnsType Description ObjectThe current instance
test.object(new Date()).isNotInstanceOf(RegExp);
See also -
hasProperty(property, value)
-
Assert that the
actual
tested object 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 .object({foo: 'bar'}) .hasProperty('foo') .object({foo: 'bar'}) .hasProperty('foo', 'bar') ;
See also -
hasNotProperty(property, value)
-
Assert that the
actual
tested object 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 .object({foo: 'bar'}) .hasNotProperty('bar') .object({foo: 'bar'}) .hasNotProperty('foo', 'baz') ;
See also -
hasOwnProperty(property, value)
-
Assert that the
actual
tested object 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 .object({foo: 'bar'}) .hasOwnProperty('foo') .object({foo: 'bar'}) .hasOwnProperty('foo', 'bar') ;
See also -
hasNotOwnProperty(property, value)
-
Assert that the
actual
tested object 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 .object({foo: 'bar'}) .hasNotOwnProperty('bar') .object({foo: 'bar'}) .hasNotOwnProperty('foo', 'baz') ;
See also -
hasProperties(properties)
-
Assert that the
actual
object 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
ReturnsType Description ObjectThe current instance
test.object({foo: 'bar', bar: 'huhu', other: 'vroom'}) .hasProperties(['other', 'bar', 'foo']);
See also -
hasNotProperties(properties)
-
Assert that the
actual
object 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
ReturnsType Description ObjectThe current instance
test.object({foo: 'bar', bar: 'huhu', other: 'vroom'}) .hasNotProperties(['other', 'foo']);
See also -
hasOwnProperties(properties)
-
Assert that the
actual
object has only the expected enumerableproperties
of 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
ReturnsType Description ObjectThe current instance
test.object({foo: 'bar', bar: 'huhu', other: 'vroom'}) .hasOwnProperties(['other', 'bar', 'foo']);
See also -
hasKey(key, value)
-
Alias of hasProperty()
Assert that the
actual
tested object 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 .object({foo: 'bar'}) .hasKey('foo') .object({foo: 'bar'}) .hasKey('foo', 'bar') ;
See also -
notHasKey(key, value)
-
Alias of hasNotProperty()
Assert that the
actual
tested object 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 .object({foo: 'bar'}) .notHasKey('bar') .object({foo: 'bar'}) .notHasKey('foo', 'baz') ;
See also -
hasKeys(keys)
-
Alias of hasProperties()
Assert that the
actual
object 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
ReturnsType Description ObjectThe current instance
test.object({foo: 'bar', bar: 'huhu', other: 'vroom'}) .hasKeys(['other', 'bar', 'foo']);
See also -
notHasKeys(keys)
-
Alias of hasNotProperties()
Assert that the
actual
object 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
ReturnsType Description ObjectThe current instance
test.object({foo: 'bar', bar: 'huhu', other: 'vroom'}) .notHasKeys(['other', 'foo']);
See also -
hasValue(expected)
-
Assert that the
actual
tested object hasexpected
value.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
ReturnsType Description ObjectThe current instance
test.object({life: 42, love: 69}).hasValue(42);
See also -
notHasValue(expected)
-
Assert that the
actual
tested object not hasexpected
value.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
ReturnsType Description ObjectThe current instance
test.object({life: 42, love: 69}).notHasValue(4);
See also -
hasValues(expected)
-
Assert that the
actual
tested object has severalexpected
values passed in an array of expected values.ParametersName Type Description expected ArrayAn array of expected values
ReturnsType Description ObjectThe current instance
test.object({life: 42, love: 69}).hasValues([42, 69]);
See also -
notHasValues(expected)
-
Assert that the
actual
tested object not has severalexpected
values passed in an array of expected values.ParametersName Type Description expected ArrayAn array of expected values
ReturnsType Description ObjectThe current instance
test.object({life: 42, love: 69}).notHasValues([43, 68]);
See also -
contains(expected)
-
Assert that the
actual
object to contain something(===)
toexpected
within depth.ParametersName Type Description expected Array[, other_excepted_values] The expected value
ReturnsType Description ObjectThe current instance
test .object({ a: { b: 10 }, b: { c: 10, d: 11, a: { b: 10, c: 11} }}) .contains({ a: { b: 10 }, b: { c: 10, a: { c: 11 }}}) .object({a: 'a', b: {c: 'c'}}) .contains({b: {c: 'c'}}) .contains({b: {c: 'c'}}, {a: 'a'}) ;
See also -
notContains(expected)
-
Assert that the
actual
object to not contain something(!==)
toexpected
within depth.ParametersName Type Description expected Array[, other_excepted_values] The expected value
ReturnsType Description ObjectThe current instance
test.object({a: 'a'}, {b: 'b', c: 'c'}).notContains({c: 'b'});
See also -
hasName(expected)
-
Assert that the
actual
object has anexpected
name.ParametersName Type Description expected StringExpected name
ReturnsType Description ObjectThe current instance
test.object(new Date(2010, 5, 28)).hasName('Date');