- In this section, we're going to see how to test
for truth and falsehood in Jest.
So we have two matchers:
toBeTruthy and toBeFalsy.
So, if you are quite new to JavaScript,
you'd be quite surprised
at how JavaScript interprets truth and falsehood.
It has... a rather... vague definition
or a rather loose definition
of what's true and what's false.
So, the key point with these tests
is to know what does truth actually mean,
and falsehood, in JavaScript.
Okay.
So, I have a test suite we're gonna look at.
Similar to the other sections,
it's going to be in the StandardJestMatchers folder.
example.test.js.
I've got a test suite that contains my tests
that match for truth or falsehood.
So, let's have a quick look in the file system.
Okay, so in my file system, in Lesson02,
in the StandardJestMatchers folder.
Open that folder in the code editor.
And it looks like this.
We have our tests in here, example.test.js.
The suite we are looking at for this section is this one:
describe matchers for truth and falsehood.
And in there I have two tests.
Would you believe it?
I have a test that exercises the toBeTruthy matcher.
And then I have a test exercises the toBeFalsy matcher.
Okay, so truthy and falsy
are definitely a concept in JavaScript.
Okay, so...
I guess the first thing we should do
is to actually run those tests.
First of all, to make sure
that they all actually work successfully.
So let's do that first.
So, npm run test -- -t truth.
Okay, so that'll select my truth and falsehood suite.
And it'll run the two tests in that suite.
Let's just verify that they actually work properly.
And then we'll have a look at the code.
So you have to be patient when you're running your tests.
And it's located my truth or falsehood suite.
And it's run those two tests,
and they've both worked.
Very good.
Let's have a look at the code.
So first of all, toBeTruthy,
it tests for a truthy value.
So, the key point here,
what is a truthy value actually in JavaScript?
Okay, so basically... (snickers)
Basically, anything is truthy if it's not falsy.
Something is falsy if it is either...
So these are the falsy values:
False.
Any zero number.
An empty string.
A null value.
An undefined value.
Or not a number.
Any of these are considered to be falsy.
Truthy is everything else.
Everything apart from these values
is considered to be truthy.
Have a look at this example here.
I've declared a bunch of variables.
All of these variables are example of truthy values.
So, obviously, the value true is truthy.
A non-zero number is truthy.
A non empty string is truthy.
Now, interestingly, an empty object is actually true.
You might expect that to be false
in the same way that an empty string is false, but no.
An empty object is actually a true value, truthy.
As is an empty array.
Okay, so that's an interesting difference.
An empty string is considered false,
but an empty object or an empty array is considered true.
So if I check a, b, c, d, e, f, g.
All of these will give me a truthy value.
All of these are considered to be true values
in the JavaScript view of truth.
And then, toBeFalsy is the opposite.
A falsy value is any value which is false,
any zero-based number, an empty string,
either null, undefined, or not a number.
So let's have a look.
So a, false, is obviously a falsy value.
Any zero-based number is false.
Whether it's a 0,
or -0,
or a bigint 0,
or a bigint of -0,
or a float.
Okay, so any zero-based number is false.
An empty string is false.
Null, undefined and not a number are also false outcomes.
So if you wrote these in a statement,
they'd give you a false outcome.
So when I check a, b, c, d, e, f, g here,
they are all falsy.
Okay, so, toBeTruthy and toBeFalsy, straightforward.
You just need to be careful to understand
what does truth and falsehood actually mean
in the JavaScript universe.