- [Instructor] So, you have learnt how to use
Jest matchers to test primitive data types
like strings and numbers.
It's now time to dive into the world of arrays and objects.
Just as with strings or numbers,
Jest comes packaged with handy matchers
for writing assertions for objects and arrays.
We will be taking a look at a couple of them in this video.
So, without wasting any time, let's dive in.
Follow along with this video.
You need the 02_04b branch of the project.
So make sure you are on the 02_04b branch.
For this exercise,
two additional properties are now present
in the utilities model.
A countries collection array,
which holds string names of five countries
and a user object, John Doe,
which contains bio-data properties
of a fictitious user name, you guessed it, John Doe.
Just like the 'addNumbers' function,
these new properties
have been included in the models exports.
So, let's start adding tests for these properties.
Inside the test file,
create a new file with the name 'object.js'.
To begin writing the test suit,
import the country's collection and John Doe objects
from the utilities model.
Next, add a text case
to check if the country's collection
contains a country named Nigeria.
For this, the 'toContain' Jest matcher will be used.
The 'toContain' matcher checks if an array contains
at least one instance of the given argument.
Save the file and let's take this test for spin.
At the root of the project,
use the test script to run the newly added test suit.
As expected,
Jest reports a passing result for the test suit.
Let's take for a country that does not exist in the array,
say France,
then observe how Jest reports it.
Replace the argument of the 'toContain' matcher
with the string 'France'.
Save the file
and return to the command line to run the test once again.
As expected, the test fails because the string France
is not contained in our countries array.
Jest prints out the expected string to be found,
that is France,
and below it shows the content of the array to indicate that
the search term does not exist in the array.
We will be testing the the John Doe object next
but first let's fix the array test
by reverting back to the search for the string Nigeria.
To test our John Doe object,
we'll be writing a test case that checks
if the user object contains a subset of its information.
The assertion will check if the John Doe object
has an age property equal to 26
and that the user's occupation is an accountant.
For this, we'll be passing into the 'toEqual' matcher,
the function
'expect.objectContaining'
which is a Jest helper.
The 'objectContaining' helper method returns true,
if the object we are searching contains the key value pairs
in the sub object passed to it.
Inside this function,
we just passed the subset of information we want to check.
Save this file and head over to the command line
to run the test.
Because our John Doe object
contains the key value pair searched,
our tests passed.
A change in any of the key value pairs
can cause the test to fail
if it is not contained in the John Doe object as specified.
For example, let's head back to our test file
and change occupation to programmer,
save this file
and return to the CLI to run the test once again.
You can see that the test now fail
as the occupation key value pair
is not as we have it in our John Doe object.
For more methods and functions
for testing objects and array,
I encourage you to check the official Jest documentation
and other useful examples online.
This will help you familiarize yourself with
different scenarios and conditions
for testing objects and arrays.