- In this section, we're going to see how you can test
if a particular error condition has occurred.
Obviously, this is important,
if you have industrial-strength applications.
If you call a function,
you wanna test that it's robust enough
to report an error when it should.
So, there are two different matchers you can use,
they're actually equivalent to each other.
You can either say toThrow, you know,
when I call this function,
I'm expecting it to throw an error.
So the toThrow matcher, you can use.
Also, you can use toThrowError, these are equivalent.
You can either use toThrow, or toThrow error,
they mean exactly the same thing.
It's quite straightforward.
We've got some examples, usual place,
in the standard jest matchers folder, exampletest.js.
So in the demo folder, lesson two, standard jest matchers.
And if you open that in your code editor,
it looks like this.
And here's my example test file.
It contains the various test suites
that we've been lookin' at so far in the lesson.
And then at the end,
there's a suite that matchers for errors.
Quite a simple example at the moment,
it just has a single test, that checks if errors occur.
So as before, the first thing I'm going to do
is to run that suite
to make sure that I get back the correct result.
So in the standard jest matchers folder, in here.
NPM run test parameters minus T errors.
And it'll find my test suite, my error-related test suite.
It'll run the one and only test in there.
And that should report back successfully.
There we go, so, matchers for errors.
The one test has passed.
Okay, so that's good, let's have a look at that example.
So we ran it like so.
And here's the code, in the test, using toThrow,
you can test whether a block of code has thrown an error.
So what you do is you have to,
the code that you want to test,
you have to enclose it in a function,
otherwise toThrow won't work.
Okay, so the code that you test
then must be wrapped up inside a function.
And here's my example.
I've got two functions, actually.
In fact, they're two lambdas, or arrow functions.
I've got a badFunc.
Here's my arrow function, or a lambda.
If I call this function, it'll fail,
because this is an undefined variable.
Okay, so I get an undefined,
it'll basically raise an exception, or throw an error,
because this is an undefined variable.
So that's a bad function.
When I call that function, when you specify the function,
that function, when it's invoked, will throw an error.
When I call the good function, this is a good function.
When that function gets invoked,
I'm expecting it not to throw an error.
Okay, so this is simple usage.
A simple way to check whether a function
does throw or doesn't throw an error.
So, it turns out that when you call toThrow,
you can actually pass an optional argument.
We didn't do that on the previous example.
But you can pass an optional argument.
And that allows you to be more rigorous
in your error checkin'.
So for example, you can say, this error that occurred.
Is the error object a particular kind of object?
Okay, so was it a bank balance error,
or was it a customer authentication error?
You can actually check what type of error object was thrown.
You can also check the error message.
So when you throw an error in JavaScript,
you specify an error message.
You can say, does the error message match this string,
insufficient funds?
You know, that kinda thing.
You can check if the error message
matches a regular expression.
So, does the error message include this piece of text,
plus other stuff,
but does it match a particular subset
of a regular expression?
Does it have a substring?
These techniques are quite straightforward to do,
we'll have a look at those in more detail in Lesson Three.