Setup and Teardown
Often while writing tests you have some setup work that needs to happen before tests run, and you have some finishing work that needs to happen after tests run. Jest provides helper functions to handle this.
#
Repeating Setup For Many TestsIf you have some work you need to do repeatedly for many tests, you can use beforeEach
and afterEach
.
For example, let's say that several tests interact with a database of cities. You have a method initializeCityDatabase()
that must be called before each of these tests, and a method clearCityDatabase()
that must be called after each of these tests. You can do this with:
beforeEach
and afterEach
can handle asynchronous code in the same ways that tests can handle asynchronous code - they can either take a done
parameter or return a promise. For example, if initializeCityDatabase()
returned a promise that resolved when the database was initialized, we would want to return that promise:
#
One-Time SetupIn some cases, you only need to do setup once, at the beginning of a file. This can be especially bothersome when the setup is asynchronous, so you can't do it inline. Jest provides beforeAll
and afterAll
to handle this situation.
For example, if both initializeCityDatabase
and clearCityDatabase
returned promises, and the city database could be reused between tests, we could change our test code to:
#
ScopingBy default, the before
and after
blocks apply to every test in a file. You can also group tests together using a describe
block. When they are inside a describe
block, the before
and after
blocks only apply to the tests within that describe
block.
For example, let's say we had not just a city database, but also a food database. We could do different setup for different tests:
Note that the top-level beforeEach
is executed before the beforeEach
inside the describe
block. It may help to illustrate the order of execution of all hooks.
#
Order of execution of describe and test blocksJest executes all describe handlers in a test file before it executes any of the actual tests. This is another reason to do setup and teardown inside before*
and after*
handlers rather than inside the describe blocks. Once the describe blocks are complete, by default Jest runs all the tests serially in the order they were encountered in the collection phase, waiting for each to finish and be tidied up before moving on.
Consider the following illustrative test file and output:
#
General AdviceIf a test is failing, one of the first things to check should be whether the test is failing when it's the only test that runs. To run only one test with Jest, temporarily change that test
command to a test.only
:
If you have a test that often fails when it's run as part of a larger suite, but doesn't fail when you run it alone, it's a good bet that something from a different test is interfering with this one. You can often fix this by clearing some shared state with beforeEach
. If you're not sure whether some shared state is being modified, you can also try a beforeEach
that logs data.