object

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

Parameters
Name Type Description
expected
mixed

Expected value

Returns
Type Description
Object

The current instance

Example
var 
  obj = {},
  obj2 = obj
;

test.object(obj).isIdenticalTo(obj2);

See also

isNotIdenticalTo(expected)


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

Parameters
Name Type Description
expected
mixed

Expected value

Returns
Type Description
Object

The current instance

Example
var obj = {};
test.object(obj).isNotIdenticalTo({});

See also

isEqualTo(expected)


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

Parameters
Name Type Description
expected
mixed

Expected value

Returns
Type Description
Object

The current instance

Example
var 
  obj = {},
  obj2 = obj
;

test.object(obj).isEqualTo(obj2);

See also

isNotEqualTo(expected)


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

Parameters
Name Type Description
expected
mixed

Expected value

Returns
Type Description
Object

The current instance

Example
var obj = {foo: 'bar'};
test.object(obj).isNotEqualTo({foo: 'bar', baz: 'bar'});

See also

match(expected)


Assert actual object 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.object({hello: 'world'}).match(function(obj){
  return obj.hello == 'world';
});

See also

notMatch(expected)


Assert actual object 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.object({hello: 'world'}).notMatch(function(obj){
  return obj.hello == 'E.T';
});

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.object({hello: 'world'}).isValid(function(obj){
  return obj.hello == 'world';
});

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.object({hello: 'world'}).isNotValid(function(obj){
  return obj.hello == 'E.T';
});
 

See also

matchEach(expected)


Assert actual object to match each expected value.

Parameters
Name Type Description
expected
Array
String
Number
RegExp
function

Expected matches for each

Returns
Type Description
Object

The current instance

Example
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 several expected value.

Parameters
Name Type Description
expected
Array
String
Number
RegExp
function

Expected value that must not match

Returns
Type Description
Object

The current instance

Example
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 an array (using typeof operator).

Returns
Type Description
Object

The current instance

Example
test.object([]).isArray();

See also

isRegExp()


Assert that the actual object is an instance of RegExp.

Returns
Type Description
Object

The current instance

Example
test
  .object(/[0-9]+/)
    .isRegExp()

  .object(new RegExp('ab+c', 'i'))
    .isRegExp()
;

See also

isNotRegExp()


Assert that the actual object is not an instance of RegExp.

Returns
Type Description
Object

The current instance

Example
test.object(new Date()).isNotRegExp();

See also

isDate()


Assert that the actual object is an instance of Date.

Returns
Type Description
Object

The current instance

Example
test.object(new Date()).isDate();

See also

isNotDate()


Assert that the actual object is not an instance of Date.

Returns
Type Description
Object

The current instance

Example
test.object(/[0-9]+/).isNotDate();

See also

isArguments()


Assert that the actual object is the arguments object provided in a function.

Returns
Type Description
Object

The current instance

Example
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 the arguments object provided in a function.

Returns
Type Description
Object

The current instance

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

Returns
Type Description
Object

The current instance

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

Returns
Type Description
Object

The current instance

Example
test.object({hello: 'Nico'}).isNotEmpty();

See also

hasLength(expected)


Assert that the actual object has a length property equal to expected number.

Parameters
Name Type Description
expected
number

Expected length

Returns
Type Description
Object

The current instance

Example
test.object({foo: 'bar', other: 'baz'}).hasLength(2);

See also

hasNotLength(expected)


Assert that the actual object has not a length property equal to expected number.

Parameters
Name Type Description
expected
number

Expected length

Returns
Type Description
Object

The current instance

Example
test.object({foo: 'bar', other: 'baz'}).hasNotLength(4);

See also

isEnumerable(property)


Assert that the actual object 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
test.object({prop: 'foobar'}).isEnumerable('prop');

See also

isNotEnumerable(property)


Assert that the actual object 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
  .object(Object.create({}, {prop: {enumerable: 0}}))
    .isNotEnumerable('prop')
;

See also

isFrozen()


Assert that the actual object is frozen with Object.isFrozen.

Returns
Type Description
Object

The current instance

Example
test.object(Object.freeze({})).isFrozen();

See also

isNotFrozen()


Assert that the actual object is not frozen with Object.isFrozen.

Returns
Type Description
Object

The current instance

Example
test.object({}).isNotFrozen();

See also

isInstanceOf(expected)


Assert that the actual object 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.object(new Date()).isInstanceOf(Date);

See also

isNotInstanceOf(expected)


Assert that the actual object 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.object(new Date()).isNotInstanceOf(RegExp);

See also

hasProperty(property, value)


Assert that the actual tested object 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
  .object({foo: 'bar'})
    .hasProperty('foo')
    
  .object({foo: 'bar'})
    .hasProperty('foo', 'bar')
;

See also

hasNotProperty(property, value)


Assert that the actual tested object 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
  .object({foo: 'bar'})
    .hasNotProperty('bar')
    
  .object({foo: 'bar'})
    .hasNotProperty('foo', 'baz')
;

See also

hasOwnProperty(property, value)


Assert that the actual tested object 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
  .object({foo: 'bar'})
    .hasOwnProperty('foo')
    
  .object({foo: 'bar'})
    .hasOwnProperty('foo', 'bar')
;

See also

hasNotOwnProperty(property, value)


Assert that the actual tested object 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
  .object({foo: 'bar'})
    .hasNotOwnProperty('bar')
    
  .object({foo: 'bar'})
    .hasNotOwnProperty('foo', 'baz')
;

See also

hasProperties(properties)


Assert that the actual object has only the expected enumerable properties. Pass an array of strings as properties.

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

Parameters
Name Type Description
properties
Array

An array of expected properties

Returns
Type Description
Object

The current instance

Example
test.object({foo: 'bar', bar: 'huhu', other: 'vroom'})
  .hasProperties(['other', 'bar', 'foo']);

See also

hasNotProperties(properties)


Assert that the actual object has not the enumerable properties. Pass an array of strings as properties.

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

Parameters
Name Type Description
properties
Array

An array of properties

Returns
Type Description
Object

The current instance

Example
test.object({foo: 'bar', bar: 'huhu', other: 'vroom'})
  .hasNotProperties(['other', 'foo']);

See also

hasOwnProperties(properties)


Assert that the actual object has only the expected enumerable properties of its own. Pass an array of strings as properties.

Does not take inherited properties into account. To do so, see hasProperties.

Parameters
Name Type Description
properties
Array

An array of expected properties

Returns
Type Description
Object

The current instance

Example
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 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
  .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 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
  .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 enumerable keys. Pass an array of strings as keys.

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

Parameters
Name Type Description
keys
Array

An array of expected keys

Returns
Type Description
Object

The current instance

Example
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 enumerable keys. Pass an array of strings as keys.

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

Parameters
Name Type Description
keys
Array

An array of keys

Returns
Type Description
Object

The current instance

Example
test.object({foo: 'bar', bar: 'huhu', other: 'vroom'})
      .notHasKeys(['other', 'foo']);

See also

hasValue(expected)


Assert that the actual tested object has expected value.

For strings it checks the text, for arrays it checks elements and for objects the property values. Everything is checked with strict equals (===).

Parameters
Name Type Description
expected
mixed

The expected value

Returns
Type Description
Object

The current instance

Example
test.object({life: 42, love: 69}).hasValue(42);

See also

notHasValue(expected)


Assert that the actual tested object not has expected value.

For strings it checks the text, for arrays it checks elements and for objects the property values. Everything is checked with strict equals (!==).

Parameters
Name Type Description
expected
mixed

The expected value

Returns
Type Description
Object

The current instance

Example
test.object({life: 42, love: 69}).notHasValue(4);

See also

hasValues(expected)


Assert that the actual tested object has several expected values passed in an array of expected values.

Parameters
Name Type Description
expected
Array

An array of expected values

Returns
Type Description
Object

The current instance

Example
test.object({life: 42, love: 69}).hasValues([42, 69]);

See also

notHasValues(expected)


Assert that the actual tested object not has several expected values passed in an array of expected values.

Parameters
Name Type Description
expected
Array

An array of expected values

Returns
Type Description
Object

The current instance

Example
test.object({life: 42, love: 69}).notHasValues([43, 68]);

See also

contains(expected)


Assert that the actual object to contain something (===) to expected within depth.

Parameters
Name Type Description
expected
Array

[, other_excepted_values] The expected value

Returns
Type Description
Object

The current instance

Example
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 (!==) to expected within depth.

Parameters
Name Type Description
expected
Array

[, other_excepted_values] The expected value

Returns
Type Description
Object

The current instance

Example
test.object({a: 'a'}, {b: 'b', c: 'c'}).notContains({c: 'b'});

See also

hasName(expected)


Assert that the actual object has an expected name.

Parameters
Name Type Description
expected
String

Expected name

Returns
Type Description
Object

The current instance

Example
test.object(new Date(2010, 5, 28)).hasName('Date');