These assertions test collections like arrays, Maps, Sets, and their properties.
✏️ Aliases:
{unknown} to be an array {unknown} to be array
Success:
expect([], 'to be an array');
expect([1, 2, 3], 'to be an array');
expect(new Array(5), 'to be an array');
Failure:
expect('hello', 'to be an array');
// AssertionError: Expected 'hello' to be an array
Negation:
expect('hello', 'not to be an array');
Success:
expect([], 'to be empty');
Failure:
expect([1, 2, 3], 'to be empty');
// AssertionError: Expected [1, 2, 3] to be empty
Negation:
expect([1, 2, 3], 'not to be empty');
✏️ Aliases:
{array} to have length {nonnegative-integer} {array} to have size {nonnegative-integer}
Success:
expect([1, 2, 3], 'to have length', 3);
expect('hello', 'to have length', 5);
Failure:
expect([1, 2], 'to have length', 3);
// AssertionError: Expected [1, 2] to have length 3
Negation:
expect([1, 2], 'not to have length', 3);
Functionally equivalent to not to be empty
.
Success:
expect([1, 2, 3], 'to be non-empty');
expect('hello', 'to be non-empty');
Failure:
expect([], 'to be non-empty');
// AssertionError: Expected [] to be non-empty
Negation:
expect([], 'not to be non-empty');
✏️ Aliases:
{array} to contain {any} {array} to include {any}
Success:
expect([1, 2, 3], 'to contain', 2);
expect(['a', 'b', 'c'], 'to include', 'b');
Failure:
expect([1, 2, 3], 'to contain', 5);
// AssertionError: Expected [1, 2, 3] to contain 5
Negation:
expect([1, 2, 3], 'not to contain', 5);
✏️ Aliases:
{Map} to contain {any} {Map} to include {any}
Success:
const map = new Map([
['key1', 'value1'],
['key2', 'value2'],
]);
expect(map, 'to contain', 'key1');
expect(map, 'to include', 'key2');
Failure:
const map = new Map([['key1', 'value1']]);
expect(map, 'to contain', 'key3');
// AssertionError: Expected Map to contain 'key3'
Negation:
expect(map, 'not to contain', 'key3');
Success:
const map = new Map([
['a', 1],
['b', 2],
]);
expect(map, 'to have size', 2);
Failure:
const map = new Map([['a', 1]]);
expect(map, 'to have size', 2);
// AssertionError: Expected Map to have size 2
Negation:
expect(map, 'not to have size', 2);
Success:
expect(new Map(), 'to be empty');
Failure:
const map = new Map([['a', 1]]);
expect(map, 'to be empty');
// AssertionError: Expected Map to be empty
Negation:
expect(map, 'not to be empty');
✏️ Aliases:
{Set} to contain {any} {Set} to include {any}
Success:
const set = new Set([1, 2, 3]);
expect(set, 'to contain', 2);
expect(set, 'to include', 3);
Failure:
const set = new Set([1, 2, 3]);
expect(set, 'to contain', 5);
// AssertionError: Expected Set to contain 5
Negation:
expect(set, 'not to contain', 5);
Success:
const set = new Set([1, 2, 3]);
expect(set, 'to have size', 3);
Failure:
const set = new Set([1, 2]);
expect(set, 'to have size', 3);
// AssertionError: Expected Set to have size 3
Negation:
expect(set, 'not to have size', 3);
Success:
expect(new Set(), 'to be empty');
Failure:
const set = new Set([1, 2, 3]);
expect(set, 'to be empty');
// AssertionError: Expected Set to be empty
Negation:
expect(set, 'not to be empty');
Success:
expect(new Set(), 'to be a Set');
expect(new Set([1, 2, 3]), 'to be a Set');
Failure:
expect([1, 2, 3], 'to be a Set');
// AssertionError: Expected [1, 2, 3] to be a Set
Negation:
expect([1, 2, 3], 'not to be a Set');
✏️ Aliases:
{WeakMap} to contain {object | symbol} {WeakMap} to include {object | symbol}
Success:
const obj = {};
const wm = new WeakMap([[obj, 'value']]);
expect(wm, 'to contain', obj);
Failure:
const obj1 = {},
obj2 = {};
const wm = new WeakMap([[obj1, 'value']]);
expect(wm, 'to contain', obj2);
// AssertionError: Expected WeakMap to contain object
Negation:
expect(wm, 'not to contain', obj2);
Success:
expect(new WeakMap(), 'to be a WeakMap');
Failure:
expect(new Map(), 'to be a WeakMap');
// AssertionError: Expected Map to be a WeakMap
Negation:
expect(new Map(), 'not to be a WeakMap');
✏️ Aliases:
{WeakSet} to contain {object | symbol} {WeakSet} to include {object | symbol}
Success:
const obj = {};
const ws = new WeakSet([obj]);
expect(ws, 'to contain', obj);
Failure:
const obj1 = {},
obj2 = {};
const ws = new WeakSet([obj1]);
expect(ws, 'to contain', obj2);
// AssertionError: Expected WeakSet to contain object
Negation:
expect(ws, 'not to contain', obj2);
Success:
expect(new WeakSet(), 'to be a WeakSet');
Failure:
expect(new Set(), 'to be a WeakSet');
// AssertionError: Expected Set to be a WeakSet
Negation:
expect(new Set(), 'not to be a WeakSet');