Skip to content

v1.5.0 - iterableOf and recordOf!

Latest
Compare
Choose a tag to compare
@EskiMojo14 EskiMojo14 released this 03 Mar 12:04
· 55 commits to main since this release

This minor release adds matchers for iterables and records (i.e. dictionary objects where all values are the same type).

toBeIterableOf,toBeStrictIterableOf,expect.iterableOf, expect.strictIterableOf

These matchers check that all the values in an iterable match a given value, using deep (and strict where applicable) equality.

For example, to check every value in an array is a number:

expect([1, 2, 3]).toBeIterableOf(expect.typeOf("number")); // or expect.any(Number)

toBeRecordOf, toBeStrictRecordOf, expect.recordOf, expect.strictRecordOf

These matchers check that all values in an object match a given value, using deep (and strict where applicable) equality.

For example, to check every value in an object is a number:

expect({ a: 1, b: 2, c: 3 }).toBeRecordOf(expect.typeOf("number")); // or expect.any(Number)

Optionally, you can pass two expected values, and the first will be used to check the key instead.

expect({ a: 1, b: 2, c: 3 }).toBeRecordOf(
  expect.oneOf(["a", "b", "c"]),
  expect.typeOf("number"),
);

Note that only enumerable string keys are checked, as the matcher iterates using Object.entries.

The naming of this matcher comes from Typescript's Record type.