jest-extended icon indicating copy to clipboard operation
jest-extended copied to clipboard

Add asymmetric number matchers

Open Pajn opened this issue 6 years ago • 1 comments

(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.

Pajn avatar Mar 28 '18 08:03 Pajn

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

mattphillips avatar Apr 16 '18 15:04 mattphillips