- [Male Speaker] Now that we know what and where
to unit test, let's write some tests.
Switch over to VS code.
In the Explorer view, navigate to lib,
then schema, right click and select new file.
The name will be reservation.test.js.
Press enter when complete.
We'll start by including the file that we want to test,
the reservation schema.
Const reservation equals require
and then the current directory,
reservation, add a trailing semi-colon.
And then a new line.
Let's describe the test suite
for what we're going to be testing
using the describe function.
So we're going to use describe and then single quote
and then combine date time.
Then I'm going to use an arrow function expression.
The first test will be about valid input.
Use the it function
and then be descriptive about what's happening.
Should return an ISO
86 01 date and time with valid input.
Then an arrow function expression.
Here's the given part.
It takes a date.
So const date equals 2017/07/10
then a semi-colon and the time.
So const time equals 0602 AM.
Then a semi-colon.
Add a new line.
I'm going to break this into
an actual unexpected for readability.
I'm also going to set up the test to fail
so we can learn to understand the output at Jest.
So const expected equal single quote
fail/06/10T0602
00.000Z
and then a trailing semi-colon.
I used UTC mode to avoid time zone bugs, by the way.
So that Z at the end is intentional.
The actual value is going to be the output of the method.
So const actual equals
reservation.combinedateandtime
I'm going to give it the arguments date,time.
The last piece is the expect statement.
Add a new line, then start with expect
and then give it the parameter of the actual value.
Expect includes match your methods,
which validate different things.
.2= is a matcher which I will use to compare
the expected to the actual.
Add a semi-colon and that's it.
Add a trailing semi-colon here and here and save the file.
Let's write one more test.
This time testing for failure.
As you recall, tests should describe
the failure of the system.
Therefore let's add a new line and then
it parentheses should return no
on a bad date and time.
Give it an arrow function expression.
This describes the expectation.
So given a funky date and time, so const date equals
then single quote exclamation point at #$
which is just junk and add a trailing semi-colon.
And then const time equals single quote fail
which is frankly easier to type.
And then a semi-colon.
Include a new line.
I'm going to write this slightly differently
by just writing the whole statement in one line.
I'm also going to use a different matcher
to check for no.
So I'm going to expect the reservation.combine
date and time
to take a date and time.
Then I'm expecting it to be no.
Execute the function.
And then semi-colon.
Excellent.
That's two tests specs.
Add a trailing semi-colon and then save the file.
Now that we have a suite of tests, how do we execute them?