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:
to be a record
,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:
to have keys <array>
,to have properties <array>
,to have 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']);
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');
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');
Aliases:
to satisfy <object>
,to be like <object>
"To satisfy" is a wonky special loose "deep equal" assertion. It is similar to AVA's t.like()
or Jest's expect.objectContaining()
. It checks that the actual object contains at least the properties and values specified in the expected object. It ignores any additional properties.
In addition, any regular expression in a property value position will be used to match the corresponding actual value (which will be coerced into a string). This makes it easy to assert that a string property contains a substring, starts with a prefix, or matches some other pattern.
Success:
expect({ a: 1, b: 2, c: 3 }, 'to satisfy', { a: 1, b: 2 });
expect({ name: 'John', age: 30 }, 'to be like', { name: 'John' });
// Using regular expressions in property values
expect(
{
email: 'user@example.com',
phone: '+1-555-0123',
id: 12345,
},
'to satisfy',
{
email: /^user@/,
phone: /^\+1-555/,
id: /123/,
},
);
Failure:
expect({ a: 1 }, 'to satisfy', { a: 1, b: 2 });
// AssertionError: Expected { a: 1 } to satisfy { a: 1, b: 2 }
Negation:
expect({ a: 1 }, 'not to satisfy', { a: 1, b: 2 });