- By default, Jest imposes a maximum timeout of 5 seconds
for a test to complete.
Which is reasonable for most tests
in a unit-test environment.
On the other hand, if you do have a test that takes longer
than 5 seconds, you have configure Jest to wait longer
so that it doesn't give up the ghost.
So we're going to see how to do that in this lesson,
in this section.
If you go into the TestingCallbacks_Timeouts folder,
this is what it looks like, in the code listings.
If you open up that folder in the code editor,
you'll see we have test code here.
The code we're gonna actually test here,
let's look at that function first.
Here is a very slow operation.
It waits 8 seconds, and then after 8 seconds,
it calls the callback function.
So, if I try to test that function, it's gonna blow up.
Jest is gonna give up after 5 seconds.
It can't bother to wait beyond that time.
So, we're gonna see how to configure Jest to wait longer.
Okay, so, here's the function we just looked at,
verySlowOperation, it has an 8-second time span
before it completes.
Okay, and here's my test, as you can see here.
By default, Jest will allow 5 seconds
for this to complete.
So, I've got the done prompter here,
I seem to be doing things correctly.
When this test executes, it calls that slow operation.
Ideally, after 8 seconds, the testResult callback will
be evoked with the return value.
And at that point, I will tell Jest, "I'm done here."
The problem is that it's going to take more than 5 seconds
for this callback to occur,
and Jest doesn't wait that long.
So, let's see what happens if we run this test
as it stands.
If I run the test using the default timeout,
that's the name of the function I just showed you,
this is the error that you'll get back.
Basically, it shows that Jest did not exit one second
after the test run completed.
It ran all test suites with test matching that timer,
but it failed, okay.
It says that there was an exceeded timeout,
there was a 5 a second timeout of 5000 milliseconds,
due in which that test didn't complete.
You could actually run that test,
so you can see the 5 seconds elapse
before the error message actually appears.
Here's my command window.
So I'll run Jest,
and as an option,
I'll pass in the name of my test:
"test using default timeout."
Okay, so, it'll call my test.
It'll give my test up to 5 seconds,
thinking "surely, that's going to be long enough."
And, well, it isn't long enough,
and "stop calling me Shirley,"
as they say in the airplane movie.
There we go.
So, it exceeded the timeout of 5000 milliseconds
for that test to complete.
Okay, so, now the problem is
if the function takes longer than 5 seconds,
then it takes longer than 5 seconds.
We have to tell Jest to wait longer.
So, the way you can do that,
is you can actually pass in a third parameter
to the test function.
The first parameter is the name of the test,
and the second parameter is basically your lambda,
you know, the function that defines your test,
and here's an extra third parameter,
we haven't seen this before.
This is the number of milliseconds
that for this particular test
that Jest will allow for it to complete.
Okay, so, this test here "test using specific timeout,"
it'll allow 10 seconds for that test to complete,
and that should be enough.
So, if I ran that test function now,
"testing using specific timeout,"
then it should complete successfully.
I'm just gonna actually do that live,
just to prove the point.
Okay, so, "test using," this test function was called
"test using specific timeout."
So, it'll allow up to 10 seconds.
My function actually takes 8 seconds,
at which point, it calls back,
and then I tell Jest, "we're done now."
So, in 8 seconds, I'm in the process of transpiring,
and then it finished.
Okay, so, there we are on a function-by-function basis,
you can specify the timeout you want
for an individual function to occur.
Now, if you have lots of tests,
and they all need to have an extended timeout,
then you can define kind of like a global timeout,
like this.
In some kind of setup code, you'd call "jest.setTimeout,"
and this would then apply for all functions,
all test functions by default.
This is the new default for all my test functions.
Okay, so, make that function call somewhere
in your test code.
I put in the file called "setupTests.js,"
there's nothing special about that file name,
you do have to tell Jest about that file name,
and we've seen before,
in your Jest config file- that is important,
that file name- you say:
"this is the code I want to run to configure Jest."
So, you give it the name of your setupTests file-
or multiple files if you want to-
it'll then configure that test, that timeout for all tests,
and we're in business.
When I have done that, in the demos,
you might be wondering why
that test timeout didn't actually kick in.
I do have a setupTests, but it's commented out.
For the purposes of the demo, that statement was commented.
So I'll uncomment it.
Okay, so, that now applies by default
for all tests.
Don't forget you have to tell Jest about that file.
So in your Jest config,
you do have to point it to that file,
like I've done.
And now, all tests will be subjected to set
10-second timeout.
So, in my tests, this first test,
that didn't have timeout,
will now default to ten.
Okay.
And that one has 10 specified anyway.
And this test here doesn't specify a timeout,
it will also default to 10 seconds.
All tests, now, will have a default of 10 seconds.
So, I can run all the tests,
and they should all work.
That's the theory, so I run all the tests like this,
and we should find that all of those 3 tests work fine.
The proof of the pudding is in the eating, so,
let's actually try that.
So, "jest."
So, it's going to run, it's going to allow 10 seconds
for each test by default, so this is gonna take
probably around 24 seconds,
by the time it allows 8 seconds for the first test,
8 seconds for the second one,
8 seconds for the third one,
and then after 24 seconds, it should say,
"everything's worked fine."
Okay, so, I think we must getting quite close
to the 24 seconds now, the excitement is growing.
24 seconds seems like an awful long time
when you're waiting for the finish.
Oh, there we go.
Okay, so, you can either set a global timeout,
or you can set a timeout on a test-by-test basis.
You might find that some tests need longer,
and some tests need shorter.
So, either pass in a third parameter -t test function
on a case-by-case basis, or set a global timeout
like we've seen in this last example.