Miscellaneous custom Jest matchers
MIT License
Bot releases are hidden (Show)
Published by EskiMojo14 8 months ago
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.
Published by EskiMojo14 8 months ago
This minor release adds new matchers for iterables. Namely:
toSatisfySequence
and expect.sequence
, which match against an iterable using predicates:
expect([1,2,3]).toSatisfySequence(
(x) => x === 1,
(x) => x === 2,
(x) => x === 3,
)
expect({ value: [1,2,3] }).toEqual({
value: expect.sequence(
(x) => x === 1,
(x) => x === 2,
(x) => x === 3,
),
})
toEqualSequence
, toStrictEqualSequence
, expect.sequenceOf
and expect.strictSequenceOf
which compare each item of an iterable deeply:
expect([1,2,3]).toEqualSequence(1,2,3)
expect([1,2,3]).toStrictEqualSequence(1,2,3)
expect({ value: [1,2,3] }).toEqual({
value: expect.sequenceOf(1,2,3)
})
expect({ value: [1,2,3] }).toEqual({
value: expect.strictSequenceOf(1,2,3)
})
The examples are shown with arrays for ease of understanding, but any iterable will work:
function* countToThree() {
yield 1;
yield 2;
yield 3;
}
expect(countToThree()).toEqualSequence(1, 2, 3)
There can be more received items than expected items/predicates, but there needs to be at least the same amount.
expect([1,2,3]).toEqualSequence(1,2) // fine
expect([1,2]).toEqualSequence(1,2,3) // not fine
Published by EskiMojo14 8 months ago
This minor release:
expect.oneOf([1,2,3])
to asymmetrically match against a number of items - each is compared using deep equality.toBeEnum(Enum)
and expect.enum(Enum)
matchers to check a value is from a given enum
Full Changelog: https://github.com/EskiMojo14/mix-n-matchers/compare/v1.2.0...v1.3.0
Published by EskiMojo14 8 months ago
This minor release adds Vitest support, and some new matchers.
This release adds support for Vitest, by adding an explicit dependency on jest-matcher-utils
for utilities provided by Jest but not Vitest.
See the README for the setup guide.
expect.typeOf
This asymmetric matcher checks that a given value has a given typeof
.
expect(getPost()).toEqual({ title: expect.typeOf("string") })
expect.arrayContainingOnly
This asymmetric matcher checks that a given array only contains values from an expected array.
Values can be duplicate or omitted, but all values present must match.
expect([1, 2]).toEqual(expect.arrayContainingOnly([1, 2, 3])) // passes
expect([1, 2, 3]).toEqual(expect.arrayContainingOnly([1, 2])) // fails
This is different to expect.arrayContaining
, which checks that all expected values are present and allows for other values.
expect([1, 2]).toEqual(expect.arrayContaining([1,2,3])) // fails
expect([1, 2, 3]).toEqual(expect.arrayContaining([1, 2])) // passes
expect.objectContainingOnly
This asymmetric matcher checks that a given object only contains matching keys from an expected object.
Keys can be omitted, but keys present must match.
expect({ a: 1 }).toEqual(expect.objectContainingOnly({ a: 1, b: 2 })) // passes
expect({ a: 1, b: 2 }).toEqual(expect.objectContainingOnly({ a: 1 })) // fails
This is different to expect.objectContaining
, which checks that all expected keys are present and allows for other keys.
expect({ a: 1 }).toEqual(expect.objectContaining({ a: 1, b: 2 })) // fails
expect({ a: 1, b: 2 }).toEqual(expect.objectContaining({ a: 1 })) // passes
Full Changelog: https://github.com/EskiMojo14/mix-n-matchers/compare/v1.1.1...v1.2.0
Published by EskiMojo14 12 months ago
This release adds a new entry point to support @jest/globals
usage - you can now import from "mix-n-matchers/jest-globals"
for all the matchers to be added.
If you're only using some matchers, your usage will be the same as previous - just import expect from "@jest/globals"
and call expect.extend()
as usual.
See README for a guide on Typescript setup.
NOTE: the jest-globals
entry point is broken in v1.1.0 due to a bundling issue. Use v1.1.1, which fixes this.
Full Changelog: https://github.com/EskiMojo14/mix-n-matchers/compare/v1.0.1...v1.1.1
Published by EskiMojo14 about 1 year ago
Should have README a little closer! This patch release fixes a usage example in the README.
Published by EskiMojo14 about 1 year ago
The library currently contains an asymmetric matcher for comparing by reference (via Object.is
) and a number of symmetric matchers for checking mock function context (this
).