Jest Loose Matcher
Loosely matches any value
Install
npm install --save-dev jest-loose-matcher
How to use
Import any comparator from jest-loose-matcher
and use it in combination with toLooselyEqual
method:
const anything between approximately = ; ;
You can also match deep nested objects and arrays:
const oneOf atLeast atMost = ; ;
Default comparators
List of default comparators:
anything
. Eg.:anything()
.approximately
. Eg.:approximately(new Date('2014-03-11T16:41:32.217Z'), 3 * 60 * 60 * 1000)
.atLeast
. Eg.:atLeast(50)
.atMost
atMost(100)
.between
. Eg.:between(0, 1)
.oneOf
. Eg.:oneOf('a', 'b', 'c')
Custom comparators
A comparator is any object that has the method compare
which receives a value as the first parameter and returns an object with the keys pass
(boolean) and message
(function that returns a message). Eg.:
const isEven = { if value % 2 === 0 return pass: true '' ; return pass: false ` is not even` ; };
If you want to provide arguments to your comparator, just turn it into a function that returns an object with the method compare
:
{ return { if typeof value !== 'string' return pass: false `could not match since it is not a string` ; else if !regex return pass: false ` does not match ` ; return pass: true '' ; } ;}
Then you can use it in your loose matchers:
const isEven matches = ; ;