(silence)
- In this section we got to see how we can write
jest tests to see if a variable is null, defined,
or undefined, or not a number.
Obviously in professional code, you want to make sure
that your variables are null when they should be
and not null when they shouldn't be.
So undefined and so on.
So the examples for this section are located here
in lesson two in the Standard Jest Matchers folder.
Open up example test .JS and if you do that
you'll find there's a test suite that checks
for null, undefined, and none.
Okay.
So let's just show you where that is on the file system.
So in lesson two, you wanna have look in the
Standard Jest Matchers folder and if you open
that folder in a code editor it looks like this.
We have an example test and in there
there's a test suite here it is.
Matchers for null, undefined, or not a number.
In this test suite I have four tests for null,
defined, undefined, and none.
Okay.
So that's good.
We'll have a look that code in a moment.
To run those tests, usual kind of arrangement
either using yarn or node package manager.
Run the test and look for the null test suite specifier.
So look for the test suite that contains the null keyword
or the null descriptor and it'll run that test suite.
Okay.
So I'll do that initially before we actually look
at the code NPM run test and then options.
I'm gonna pass the minus T option.
I'm looking for the test suite that
contains the null word.
So it should find my test suite and it should run
the four tests in that test suite and they should all pass.
That's the idea.
Okay, so it ran the tests in this test suite
because it matched the null word as I asked it to up here
and those four tests have actually worked fine.
So let's have a look at the code.
So first of all then to be null, checks
if a variable is null quite obvious
really.
Here's an example.
A is null, I'm expecting a to be null.
It is.
I'm expecting B not to be null.
Okay.
So you can check for something to be null
or not to be null like so.
And similar idea if you want to check
if a property is defined, does an object
have a property.
Have a look at this example.
This is, oh, it's me.
I have an object here.
The person's name is Andy.
Oh, the age actually I'm 57 now.
So I should increment my age here that's depressing
isn't it?
- Anyway, my person object has a name
property and an age property.
I'm expecting that the person name property is defined.
Okay, in other words, there is a name property
on the object and it has a value.
Okay.
So that's a good sanity check.
Yes, my personal object does have a name property
and there it is.
The opposite, checking for a missing property
using to be undefined checks if a property is undefined.
So here we go.
Here's the same person again.
How remarkably young that person is.
I'm check taking that person
and I'm checking that, that person's
that person object does not have a company jet property.
As far as I'm aware I don't own a company jet
although that would be quite nice, but I don't have one yet.
So this person object, if we check the company jet property
the company jet property is undefined on that object.
Okay.
So in other words this object does not have that property.
That's quite useful.
One day I might be able to come back
and flip this to be defined property.
Maybe.
Right.
What about not a number?
Not a number occurs sometimes when you're doing
various mathematical operations
it fails basically, and you get back a non-numeric result.
So have a look at this.
If your maths is quite up to date.
If you try to take the square root of a negative number,
you can't take the square root of a negative number.
Not unless you start talking about complex numbers anyway.
So when you call math dot square root
if you give it a negative parameter,
it gives you back none.
Okay.
It's not a real number.
So the square root of minus 25
I'm expecting to be not a number.
If I take the square root
of a regular number, then that's fine.
I'm expecting that not to be not a number.
Okay.
So the square root of minus 25 is effectively invalid.
The square root of plus 25 is not invalid.
In other words is valid.
Okay, so that can be quite useful.
If you wanna check
that your numbers are actually real correct numbers.