- So far, the examples we've been looking at
have been quite simple.
We've been using the toBe () function
to test the result of an operation.
So for example, if we had an add function, from before,
we import the add function in our test.
We call the add function with some parameters,
and we say that we expect the result to be three.
So what we're gonna do in this lesson is take a closer look
at these matches toBe () as an example
of a Jest matcher special function predefined
in the Jest library to do an equality check.
So as it happens, when you call toBe (), it takes the result
of the expect, you know, whatever you put in here.
And it checks that result using
the equality operator triple equals to check
if that result is identical to that value.
Okay. So that's obviously a very simple matcher.
ToBe () is one of the most widely used matchers,
but it's just one of many.
So as it turns out, there are quite a rich set
of different types of tests you can perform in a Jest test.
So the, you can test equality as we've just seen.
You can test if a result is in a certain range.
So for example, is a number between five and 10.
You can test if a result from a function is null,
or undefined, or not a number.
You can check for a bullying result.
If you call a function, you can test
if the result is true or false.
You can test the properties in an object.
You can test for collections.
You can say, does the collection have a length of five,
or does the collection contain item X, Y, Z?
And you can test for errors sometimes.
You know, when you call a function,
if you pass an invalid parameter into a function,
it should raise an error,
and you can test that it does raise an error when expected.
So basically you are testing that your code is robust enough
to raise an error when it should do, okay?
So that's important as well.
So these are all standard matches.
You can also define custom matches
as well as the built in tests, which are fairly useful
but also quite quite technical.
You can define a more semantic custom matcher
for a domain specific condition.
So for example, something, if you're working in a,
if you're working in a school examination system,
you could check that an exam mark is valid.
Is it between zero and a hundred?
If you are doing, you know, physics,
you can check that the temperature is
between absolute zero and 150 million degrees.
If you're interested in performing nuclear fusion in a lab.
So you can provide a custom matcher
which is more semantically rich
and potentially reusable across multiple tests.
You can use it again and again in multiple tests.
We'll see how to do that
towards the end of the, of the lesson.
Okay.
So, what we'll do at the beginning of the, of the lesson
in the first two sections is to take a detailed look
at the standard matches provided in Jest
so that you understand.
It's really important that you understand
what matches are available,
and that you use them properly because you,
this is the bed lock of your test in code.
Okay.
So what I've done in the demo folder, within lesson two,
I've got two different sets of demos.
The first folder I'm gonna look at, Standard Jest Matches,
takes a fairly detailed look at the standard matches I've
just been explaining.
Towards the end of the lesson,
we'll see how to define a custom matcher
for something a little bit more adventurous.
But for now, we'll concentrate on the standard Jest matches.
If I open this folder up in the code editor,
so it has, I've got a simple test file
which just shows how to use the various matches.
And we'll go through that in detail
during the rest of this lesson.
Okay. So I just open that file.
As you can see, I have a test suite
that contains various tests that use,
that do a quality checking.
If I just collapse this down, I've got another suite
that matches, shows how to match for range,
another suite that matches for null or undefined
or not a number, a test suite that shows how to match
for truth or falsehood, a test suite that shows how
to match object properties, a test suite
that matches collections, and a test suite
that matches for errors.
So as we go through the lesson, we're going to dig in
to each of these suites and explore the matches
in some detail.
Okay. So that's what it looks like
in my Standard Jest Matchers folder.
In my example test file, we have one, two, three,
a bunch of different suites, which illustrate the matches
in sufficient detail so that you can be productive.
Now, if you wanted to you could run all of these tests
all at once.
That would be great.
You're also gonna get overwhelmed with test results.
So probably what we, what a best, a better approach would be
would be to just run tests in an individual suite.
And that's the approach that we are gonna take.
Okay. So, oh, and also we'll see how
to define custom matches later on in the in this lesson.
So if we want to run tests for a particular suite,
we've seen this before.
You would do something like this.
Either yarn test, and then you give it minus T
to narrow down the tests you want to run,
and then you give it like a partial string
to match the name of your suite.
Okay. So this would run all tests in a suite
that contains equality in the suite name.
Well, as it happens, that's going to run all the tests
in this suite.
We'll have a look at those in detail in the next section.
Okay. There are several of them.
So, when I run the test, it'll run all the tests
in this suite here, just that suite.
If you prefer, you can run NPM instead of yarn.
NPM run test and then give it the, the name
of the test suite to, to execute.
So, I'm gonna do that.
Let's open a command window and run this command
to run the equality suite.
So, I'm gonna open a folder, gonna open a command window
in that folder.
And in this folder, let's do an NPM run test
and then pass in parameters into the test command,
the minus T option, and then the, the name
or the partial name for the suite that we want to run.
And make sure I spell it correctly.
So that will run the equality suite,
and it'll run all the tests in that suite.
So, we'll just give that a moment to, to get going.
Okay. So it's past the minus T equality parameters
into Jest.
It found my one and only test file.
In that test file, there are a bunch of suites.
It ran the tests in this suite.
You can see the whole past. Great.
It detected the other suites as well, but it skipped all
of those tests because I didn't want to run these suites.
I wasn't interested in running every single test.
Life is too short for that.
So, it skipped all of these suites.
The only suite that it actually executed was the suite
that contained the match for my parameter.
Okay. So what we're gonna do in the rest of the lesson
or for most of the rest of the lesson, is to dig
into these different matches and to see how they work.
You know, there are some subtle differences here,
and it is important that you understand how these all work.
So, that's the, that's the mission for the rest
of this lesson.