jest-extended
jest-extended copied to clipboard
Add asymmetric number matchers
(Copied from https://github.com/facebook/jest/issues/5880)
Do you want to request a feature or report a bug? A feature
I dumped the template as it doesn't fit feature requests very well. Hope you are okay with that.
Today we have asymmetric matchers like expect.stringMatching
which are awesome for testing dynamic data or only check data that important for the current test. However, there exists no equivalent for numbers.
I propose something like
expect.numberNear(expected, precision)
which would check that the actual value is in the range expected ± precision
.
A common use case for this would be checking a timestamp in a generated object
expect(object).toEqual({type: 'log', timestamp: expect.numberNear(Date.now(), 20)})
Maybe precision
could default to something like Math.log(expected)
so that it could be optional, however this is (maybe a big) risk that that's more confusing than helpful.
Hey @Pajn sorry for the delayed response! I like this a lot 😄
Fancy sending a PR?
Note: We have to wait for custom asymmetric matches to land in Jest's core