Skip to main content

Array

.toBeArray()

Use .toBeArray when checking if a value is an Array.

test('passes when value is an array', () => {
  expect([]).toBeArray();
  expect([1]).toBeArray();
  expect(true).not.toBeArray();
});

Open browser consoleTests

.toBeArrayOfSize()

Use .toBeArrayOfSize when checking if a value is an Array of size x.

test('passes when value is an array', () => {
  expect([]).toBeArrayOfSize(0);
  expect([1]).toBeArrayOfSize(1);
  expect(true).not.toBeArrayOfSize(1);
});

Open browser consoleTests

.toIncludeAllMembers([members])

Use .toIncludeAllMembers when checking if an Array contains all of the same members of a given set.

test('passes when given array values match the members of the set', () => {
  expect([1, 2, 3]).toIncludeAllMembers([2, 1, 3]);
  expect([1, 2, 2]).toIncludeAllMembers([2, 1]);
});

Open browser consoleTests

.toIncludeAllPartialMembers([members])

Use .toIncludeAllPartialMembers when checking if an Array contains all of the same partial members of a given set.

test('passes when given array values match the partial members of the set', () => {
  expect([{ foo: 'bar', baz: 'qux' }]).toIncludeAllPartialMembers([{ foo: 'bar' }]);
});

Open browser consoleTests

.toIncludeAnyMembers([members])

Use .toIncludeAnyMembers when checking if an Array contains any of the members of a given set.

test('passes when given array values match any of the members in the set', () => {
  expect([1, 2, 3]).toIncludeAnyMembers([2, 1, 3]);
  expect([1, 2, 2]).toIncludeAnyMembers([2]);
  expect([1, 2, 2]).not.toIncludeAnyMembers([3]);
});

Open browser consoleTests

.toIncludeSameMembers([members], fnOrKey)

Use .toIncludeSameMembers when checking if two arrays contain equal values, in any order. for better error message use the optional fnOrKey argument to specify how to determine two items similarity (e.g. the id property)

test('passes when arrays match in a different order', () => {
  expect([1, 2, 3]).toIncludeSameMembers([3, 1, 2]);
  expect([{ foo: 'bar' }, { baz: 'qux' }]).toIncludeSameMembers([{ baz: 'qux' }, { foo: 'bar' }]);
});

Open browser consoleTests

.toPartiallyContain(member)

Use .toPartiallyContain when checking if any array value matches the partial member.

test('passes when a string has a given substring', () => {
  expect([{ foo: 'bar', baz: 'qux', bax: 'zax' }]).toPartiallyContain({ foo: 'bar' });
  expect([{ foo: 'bar', baz: 'qux', bax: 'zax' }]).toPartiallyContain({ baz: 'qux' });
  expect([{ foo: 'bar', baz: 'qux', bax: 'zax' }]).not.toPartiallyContain({ foo: 'qux' });
});

Open browser consoleTests

.toSatisfyAll(predicate)

Use .toSatisfyAll when you want to use a custom matcher by supplying a predicate function that returns a Boolean for all values in an array.

test('passes when all values in array pass given predicate', () => {
  const isOdd = el => el % 2 === 1;
  expect([1, 3, 5, 7]).toSatisfyAll(isOdd);
  expect([1, 3, 4, 5, 7]).not.toSatisfyAll(isOdd);
});

Open browser consoleTests

.toSatisfyAny(predicate)

Use .toSatisfyAny when you want to use a custom matcher by supplying a predicate function that returns true for any matching value in an array.

test('passes when any value in array pass given predicate', () => {
  const isOdd = el => el % 2 === 1;
  expect([2, 3, 6, 8]).toSatisfyAny(isOdd);
  expect([2, 4, 8, 12]).not.toSatisfyAny(isOdd);
});

Open browser consoleTests

.toBeInRange(min, max)

Use .toBeInRange when checking if an array has elements in range min (inclusive) and max (inclusive).

test('passes when given array is in range', () => {
  expect([4, 5, 7, 9]).toBeInRange(4, 10);
  expect([12, 13, 15, 17]).not.toBeInRange(4, 9);
});

Open browser consoleTests