- Jest has several matches, which allow us to check
if a number is in a certain range.
They're quite straightforward.
You can check if a number is less than another
or less than or equal or greater than,
greater than or equal, or close to.
Close to is useful when you have a fraction
and you wanna specify the level
of accuracy in your comparison.
So I have examples that show these matches.
They're quite straightforward.
All of the examples are in this suite, Matches for range.
This suite is in this file in this folder.
So let me just quickly show you where you can get this code
if you want to try it for yourself.
Okay. So we're in lesson two.
We are looking to look at the standard Jest matches folder.
And if you open that folder in the code editor,
you find that we have example test in there,
contains all the code for this lesson.
Most of the stuff for this lesson.
And I have a suite here, Matches for range,
and I have five tests in that suite as it happens.
One for each of the methods that I just mentioned.
Okay.
So a test for less than, a test for less than or equal,
a test for greater than, greater than or equal, or close to.
So I'm gonna go through the code for those in a moment.
It is quite straightforward but worth doing I think.
I think probably the first thing we should do
is to actually make sure that those tests actually work.
So to run those tests
you can say either yarn test or NPM run test.
So I'm gonna do that now, open a command window,
and I'm gonna run these tests just to make sure
that they do actually work
before we have a look at the code
NPM run test
and parameters into the test command minus T range.
Okay. So basically it'll run any test suite
that has the range name in the selector.
So our suite, it'll run the five tests
that I've just mentioned and they should all work fine.
Okay. Here comes.
Yeah, there we are.
So it ran, it skipped most of the tests,
and it just ran the tests in my range suite.
And as you can see, all of those tests have succeeded
so we're good to go.
Let's have a look at the code.
So first of all, toBeLessThan,
obviously it uses the less than operator
and it works with regular numbers and
with BigInt.
BigInt, I'm not sure if you've seen this
in JavaScript.
Obviously it allows you to store very big numbers,
very big integers.
So here's my first test.
I have "a" and "b".
I'm expecting "a" to be less than "b", yes.
1.5 is less than 3.5
"C" and "d" are BigInts.
"C" is that big integer and "c" is less than "d".
Okay. So it works fine with big integers,
which is quite nice.
Not that we use BigInt much, but it's reassuring to know
that it does actually work with BigInt.
Okay, so that's toBeLessThan, toBeLessThanOrEqual,
exactly the same idea.
So have a look at this code.
"A" is 1.5.
"A" I'm expecting to be less than or equal to 1.6.
Yes, "a" is less than 1.6.
I'm expecting "a", to be less than or equal to 1.5.
Okay, it's actually equal, but that's fine.
So "a" is a regular number here.
In this case, "b" is a BigInt.
I'm expecting this BigInt to be less than or equal to that.
And it is, it's less than.
I'm expecting this to be less than or equal to that.
And it is, it's equal, but that's fine.
Right.
Well, similar idea with the greater than tests,
you can check if a number is greater than another
or equal.
So this first of all, is "a" greater than "b"?
Is "a" greater than "b", yes it is.
Is "c", is that BigInt greater than that BigInt?
Yes "c" is greater than "d".
Okay, so that's what you would've imagined.
toBeGreaterThanOrEqual, similar idea.
So I'm expecting "a" to be greater than or equal to that,
it's greater than, greater than or equal to that,
it's equal to, but that's fine.
And then the same kind of test using BigInt.
So here's a big integer.
It should be greater than or equal to that and it is.
All right so, very straightforward.
I guess the only thing that is worth particularly mentioning
is the fact that it does actually work
with these big integers as well as regular numbers.
Now, if you have a fraction, you need to be a bit
careful when you are comparing floats, because
there's an inherent inaccuracy with a floating point number.
So if you are checking or comparing or testing
floating point numbers, you should use toBeCloseTo.
If you want, rather than checking for equality.
If you want to check for equality,
you should check for toBeCloseTo.
The way it works, it takes two numbers.
Obviously you say the number that you're looking for,
and then, when you say to be close to you say,
this is the number you want to compare against.
That's the first parameter.
And the second parameter is the level of accuracy.
It's the number of digits after the decimal point
that you want to take into account.
And the default number of places is two digits
after the decimal point.
Have a look at this example, "a" and "b" are quite similar.
If I check if "a" is close to "b",
the toBeCloseTo function ideally takes two parameters,
but the second parameter defaults to two decimal places.
So here I'm comparing "a" and "b"
to two decimal places after the decimal point.
So after the decimal point, they both say 10.12, 10.12.
Okay, so to two decimal places "a" is close to "b".
If you want to pass in an actual number of decimal places.
So four digits will now be taken
into account after the decimal point.
So is "a" close to "b"?
If we look at four decimal places, one, two, three,
yes the first four digits after the decimal place there
are the same there.
It basically ignores the last digit in that case.
It only looks at the four first ones.
It doesn't round up or round down.
It just looks at those first four digits.
And the first four digits are the same.
So "a" is close to "b",
if we just look at four digits.
However, if you look at five digits,
then you can see that "a" and "b" are not.
"A" is not close enough to "b" if you look at five digits.
So I would expect "a" not to be close to "b"
if you look at five digits, okay,
they're different to each other,
if you take five digits into account.
So there we go.
Quite straightforward.
You just need to be a little bit careful I guess
when you're dealing with floating point numbers that you,
never check for absolute quality with floats.
You always use a proximity check like we've just seen.