These assertions test objects, their properties, and object-specific behaviors.
Success:
expect({}, 'to be an object');
expect({ a: 1 }, 'to be an object');
expect([], 'to be an object'); // Arrays are objects
expect(new Date(), 'to be an object');
Failure:
expect('hello', 'to be an object');
// AssertionError: Expected 'hello' to be an object
expect(null, 'to be an object');
Negation:
expect('hello', 'not to be an object');
expect(null, 'not to be an object');
✏️ Aliases:
{unknown} to be a record {unknown} to be a plain object
Success:
expect({}, 'to be a record');
expect({ a: 1, b: 2 }, 'to be a plain object');
Failure:
expect([], 'to be a record');
// AssertionError: Expected [] to be a record
expect(new Date(), 'to be a record');
Negation:
expect([], 'not to be a record');
Success:
expect({}, 'to be empty');
Failure:
expect({ a: 1 }, 'to be empty');
// AssertionError: Expected { a: 1 } to be empty
Negation:
expect({ a: 1 }, 'not to be empty');
✏️ Aliases:
{object} to have keys {array} {object} to have properties {array} {object} to have props {array} {object} to include keys {array} {object} to include properties {array} {object} to include props {array} {object} to contain keys {array} {object} to contain properties {array} {object} to contain props {array}
Success:
expect({ a: 1, b: 2 }, 'to have keys', ['a', 'b']);
expect({ name: 'John', age: 30 }, 'to have properties', ['name', 'age']);
Failure:
expect({ a: 1 }, 'to have keys', ['a', 'b']);
// AssertionError: Expected { a: 1 } to have keys ['a', 'b']
Negation:
expect({ a: 1 }, 'not to have keys', ['a', 'b']);
✏️ Aliases:
{object} to have key {keypath} {object} to have property {keypath} {object} to have prop {keypath} {object} to include key {keypath} {object} to include property {keypath} {object} to include prop {keypath} {object} to contain key {keypath} {object} to contain property {keypath} {object} to contain prop {keypath}
Tests whether an object has a property at the specified keypath using dot or bracket notation. This assertion supports complex keypath traversal including nested properties, array indices, and quoted keys.
Supported keypath formats:
'prop.nested''arr[0]''obj["key"]' or "obj['key']"'data.items[1].name'Success:
const obj = {
foo: { bar: [{ baz: 'value' }] },
'kebab-case': 'works',
items: [
{ id: 1, name: 'first' },
{ id: 2, name: 'second' },
],
};
expect(obj, 'to have key', 'foo.bar');
expect(obj, 'to have property', 'foo.bar[0].baz');
expect(obj, 'to have prop', 'kebab-case');
expect(obj, 'to have key', 'items[1].name');
expect(obj, 'to have property', 'foo["bar"][0]["baz"]');
Failure:
expect(obj, 'to have key', 'nonexistent.path');
// AssertionError: Expected object to contain keypath nonexistent.path
expect(obj, 'to have property', 'foo.bar[5].missing');
// AssertionError: Expected object to contain keypath foo.bar[5].missing
Negation:
expect(obj, 'not to have key', 'nonexistent.path');
expect(obj, 'not to have property', 'foo.bar[5].missing');
✏️ Aliases:
{object} to have exact key {string | number | symbol} {object} to have exact property {string | number | symbol} {object} to have exact prop {string | number | symbol}
Tests whether an object has an exact property key without keypath traversal. This assertion checks for direct properties on the object and supports symbols and keys that would conflict with bracket/dot notation.
Unlike to have key, this assertion:
Success:
const sym = Symbol('test');
const obj = {
simple: 'value',
'key.with.dots': 'direct property',
'key[with]brackets': 'another direct property',
[sym]: 'symbol value',
};
expect(obj, 'to have exact key', 'simple');
expect(obj, 'to have exact property', 'key.with.dots'); // literal key, not traversal
expect(obj, 'to have exact prop', 'key[with]brackets'); // literal key, not array access
expect(obj, 'to have exact key', sym); // symbol key
Failure:
const obj = { nested: { prop: 'value' } };
expect(obj, 'to have exact key', 'nested.prop');
// AssertionError: Expected object to have exact key nested.prop
// (This fails because 'nested.prop' is not a direct property)
expect(obj, 'to have exact property', 'missing');
// AssertionError: Expected object to have exact key missing
Negation:
expect(obj, 'not to have exact key', 'missing');
expect(obj, 'not to have exact property', 'nested.prop'); // no traversal
✏️ Aliases:
{object} to have a null prototype {object} to be a dictionary
Success:
const obj = Object.create(null);
expect(obj, 'to have a null prototype');
Failure:
expect({}, 'to have a null prototype');
// AssertionError: Expected {} to have a null prototype
Negation:
expect({}, 'not to have a null prototype');
✏️ Aliases:
{string | number | symbol} to be an enumerable property of {non-null} {non-null} to have enumerable property {string | number | symbol}
This accepts any non-null, non-undefined value as the second parameter.
Success:
const obj = { a: 1, b: 2 };
expect('a', 'to be an enumerable property of', obj);
Failure:
const obj = { a: 1 };
Object.defineProperty(obj, 'b', { value: 2, enumerable: false });
expect('b', 'to be an enumerable property of', obj);
// AssertionError: Expected 'b' to be an enumerable property of object
Negation:
expect('b', 'not to be an enumerable property of', obj);
Success:
const obj = { a: 1 };
Object.seal(obj);
expect(obj, 'to be sealed');
Failure:
expect({}, 'to be sealed');
// AssertionError: Expected {} to be sealed
Negation:
expect({}, 'not to be sealed');
Success:
const obj = { a: 1 };
Object.freeze(obj);
expect(obj, 'to be frozen');
Failure:
expect({}, 'to be frozen');
// AssertionError: Expected {} to be frozen
Negation:
expect({}, 'not to be frozen');
Success:
expect({}, 'to be extensible');
Failure:
const obj = {};
Object.preventExtensions(obj);
expect(obj, 'to be extensible');
// AssertionError: Expected {} to be extensible
Negation:
expect(obj, 'not to be extensible');
See {unknown} to satisfy {any} in Equality & Comparison Assertions.
See {unknown} to deep equal {any} in Equality & Comparison Assertions.