- In this section
we are going to see how to use equality matchers
in Jest to check if two values are equal.
As it turns out there are several different ways to do this.
Several different matchers you can use.
The most obvious one being, toBe, obviously.
So we're gonna look at examples of each of these matchers.
We'll see how to use the, toBe matcher.
We'll see how to use toEqual
and we'll discuss the difference.
What is the difference between toBe and toEqual?
And also we'll have look at toMatch,
which is to test if a string matches a regular expression.
And then we'll look at toMatchObject where you can check
if two objects are compatible with each other
and explain what that actually means during this session.
You can also use the "not" keyword
or the "not" operator to invert the sense of any assertion.
I'll show you examples of that as well.
So the examples we're going to look
at are in the standard Jest matchers demo folder.
So in lesson two, standard Jest matchers
we're going to be looking,
all the tests are going to be in this file.
And let me just remind you what that looks like.
So example test.js.
As I mentioned earlier,
we have a variety of different suites here.
Basically, I'm going to look at a different suite
in each section in this lesson.
So for this particular session
we are going to be looking at the matchers for equality.
And I have four different tests
in here that I'm gonna go through in this section.
Oh, in fact, I've got five.
There we go, off by one error.
I'm going to show the toBe test.
And I'm gonna basically emphasize that toBe looks
at object identity.
Then we'll have a look at the toEqual matcher.
Then we'll have a look at the toMatch matcher,
which tests a string against a regular expression.
And then we'll see the toMatchObject matcher,
which matches properties on one object against properties
on another object to see if they're compatible
with each other.
Okay, so we have five tests
we're gonna look at in this section.
Okay, so there's our test suite matchers for equality,
and we have the five tests in here.
I guess the first thing we should do is to actually
run this suite to make sure that tests actually
work properly.
So let's go to command window and just run the tests
in the equality suite to make sure that those five
tests actually do work before we go any further.
Okay, so I've opened up a command window in lesson two,
standard Jest matchers, so NPM, run test
and then parameters we're gonna pass into the test command.
The tests I'm looking to run are the tests
in the equality suite.
Ignore the other suites
and just run the tests in the equality suite.
So there are five tests altogether in that suite.
And I think they're all gonna pass.
And which way-
Yes, there we go.
So the five tests all passed and like I was saying earlier
the other tests in the other suites are skipped
because we don't want to run those.
We just want to run the equality tests.
So all the tests I'm about to show you pass.
Okay, so bear that in mind as we go through the examples.
Okay, so first of all, let's have a look at toBe.
The toBe matcher compares two values using the triple equals
operator.
Okay., so basically it's the identity comparison
in JavaScript.
So have a look at this simple example, a test here, it shows
that toBe compares using the triple equals operator.
So I have two variables here, a and b, 42,
and oh, I could have had semicolons at the end of each line.
Semicolons are optional in JavaScript these days.
So there's no great trauma there.
I'm expecting a, to be identically equal to B.
Okay, so a and B are both 42.
Yes, they're equal.
I'm expecting a, 42, not to be equal to 21.
Okay, so yes, 42 is not the same as 21.
Okay, so note the use of the "not" operator
in the second assertion to invert the sense of the test.
I'm looking for a false outcome here.
So that that is not equal to that.
Fairly straightforward.
Just to ground this example,
all the tests here are in the demo package.
Okay, so this is the code that we just looked at.
toBe compares using the triple equals operator.
That's the code that I just showed you.
So all the code that I'm gonna show in the next few minutes
you can find in here if you want to dig around afterwards.
Okay, so the next test,
it's important here to understand
that toBe, if you're dealing with objects,
it compares object identity, not the values.
So have a look at this code here.
It shows that the toBe matcher compares object identity.
Basically it's looking at the object reference
not the value inside the object.
So I've got a variable here.
I've got an object reference that points to an object,
and this object has a property.
It's my home country.
And the population of Wales is about 3 million.
So we go for quality, not quantity in Wales.
So we have about 3 million people in Wales.
Now that's one object reference that points to this object.
This is a different object reference.
This is a different object.
So country two is a different.
Happens to have the same property values,
but it's a different object.
So if you say, is country one equal to, you know,
is it the same as country one?
Does this reference refer
to the same object as that reference?
Well, yes, that reference refers to this object.
This reference also refers to the same object.
So it says, yes, they are actually the same object
the actual same object.
However, country one and country two are not equal.
Okay, so I'm saying, I'm expecting country one
not to be equal to country two.
It compares object references.
Country one is not the same object as country two.
Okay, it looks at object addresses
basically, country one is one object address,
country two is not the same.
So that's important.
It doesn't look at the value.
It just looks at where the point is appointed to.
This points to one object.
This points to a different object.
They're different objects.
They're not equal.
If you wanna check for the value of an object instead
of the, you know, the address of the object, then instead
of using toBe, you can use toEqual.
toEqual looks deeper
inside the object to compare the values rather
than you know, the addresses.
So similar example here, country one points
to one object that has some properties,
country two points to a different object
that happens to have the same property values.
If I say toEqual now, it'll be true.
Country one is this object and it has properties
name and population with these values.
Country two is a different object,
but with the same values.
toEqual looks at the values inside the objects.
They're different objects
but they contain the same values.
So toEqual says, yes, they are equal.
They contain the same values.
Okay, so it's like a deep comparison here.
Not a not shallow comparison.
It looks deeply inside these objects and says, yes
they have the same property names
and the same property values.
So they are equal in value.
Okay.
Next example toMatch, that's to do with regular expressions.
You can check if a string matches a regular expression.
So got some examples here.
So I've got a string, Oslo.
I do a lot of work in Norway and I like Oslo very much.
It's a very beautiful city.
I'd recommend it.
So that's my string.
Imagine that this string came back from a function call.
I can give it a simple string like this.
When you're doing,
here's an important point, actually, when you're doing path
imagine in JavaScript, it doesn't check
if that string is identical to that string,
it just checks to see
if this pattern appears somewhere in the message.
Does my message contain this text somewhere?
Okay, by default it's case sensitive.
So somewhere in message, does it contain this text?
And yes, it does.
You can use the slash here to formulate
a regular expression.
Regular expressions are very powerful.
If you have a dot in a regular expression
it matches any single character.
Okay, and the slash the "I"
at the end means case insensitive.
So it says, does my message,
Oslo is in Norway,
does that message match N O R W something,
something case insensitive?
Does this text appear somewhere in the message?
Yes, it does.
As long as we're bein case insensitive
then this text, N O R W something,
something is located in the string.
So that gives me, that does match.
If you look at this example,
the second example had a case insensitive comparison.
The third example, isn't case insensitive
in other words, it's case sensitive.
Okay, so in this case, this text does not match the message.
Okay, because the N is lowercase and it's case sensitive.
So if I check that message against that,
I would expect it not to match.
Okay, because the case sensitivity blows it apart
basically.
Okay,so regular expressions they're useful
when you have strings.
Last example for this section is,
if you want to match objects to see
if they contain compatible properties.
So I'll explain what that means.
Have a look at this example.
Imagine that there's a car on sale.
Okay, and the car on sale in the garage is a Mazda, a CX 30
which is a hybrid it's kind of half electric,
half petrol or gas.
And it's red.
My favorite color is red,
because that's the color of Wales.
So it's important fact to know.
That's the car that's on sale in the garage four court.
Okay, the car that I want is a Mazda
and I want it to be hybrid.
So we were actually looking to buy a hybrid car a couple
of years ago, and we wanted to buy a Mazda.
We very much like Mazdas in our family.
And I didn't really care what the model was.
Don't care, what the color is.
Red would be nice, but it's not mandatory.
All I want is to make sure that it's a Mazda that's hybrid.
So you can do this.
You can say, does this object, car on sale,
match my requirements?
And what it does
it kind of does where the properties are, the same,
it checks to see if they match each other.
It's like a partial match on properties.
This car on sale,
compare it with the car desired.
My desired car is a make Mazda.
Does this car have a make Mazda?
Yes, so far so good.
Is it a hybrid fuel type?
Yes, it is.
So those objects match the properties
that I was looking for are in this object.
I mean, there are other properties as well,
but those have ignored.
It just looks at the properties that you've asked for.
And it says, are these properties defined in the object?
Yes, they are.
Okay, so it matches.
So that's really smart.
Often in JavaScript, you have an object
with a whole bunch of properties and, you know,
who cares what all the properties are,
but I do care that these properties are equal.
So you can just check that these properties match
and the other properties are ignored.
Very useful to do that.