Setup and Teardown
If you have some work you need to do repeatedly for many tests, you can use and afterEach
hooks.
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 - 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:
beforeEach(() => {
return initializeCityDatabase();
});
In 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
hooks to handle this situation.
beforeAll(() => {
return initializeCityDatabase();
});
afterAll(() => {
return clearCityDatabase();
});
test('city database has Vienna', () => {
expect(isCity('Vienna')).toBeTruthy();
});
test('city database has San Juan', () => {
expect(isCity('San Juan')).toBeTruthy();
});
The top level before*
and after*
hooks apply to every test in a file. The hooks declared inside a describe
block apply only 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.
beforeAll(() => console.log('1 - beforeAll'));
afterAll(() => console.log('1 - afterAll'));
beforeEach(() => console.log('1 - beforeEach'));
afterEach(() => console.log('1 - afterEach'));
describe('Scoped / Nested block', () => {
beforeAll(() => console.log('2 - beforeAll'));
beforeEach(() => console.log('2 - beforeEach'));
afterEach(() => console.log('2 - afterEach'));
test('', () => console.log('2 - test'));
});
// 1 - beforeAll
// 1 - beforeEach
// 1 - test
// 1 - afterEach
// 2 - beforeAll
// 1 - beforeEach
// 2 - beforeEach
// 2 - test
// 2 - afterEach
// 1 - afterEach
// 2 - afterAll
// 1 - afterAll
Jest 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.
describe('describe outer', () => {
console.log('describe outer-a');
describe('describe inner 1', () => {
console.log('describe inner 1');
test('test 1', () => console.log('test 1'));
});
console.log('describe outer-b');
test('test 2', () => console.log('test 2'));
describe('describe inner 2', () => {
test('test 3', () => console.log('test 3'));
});
console.log('describe outer-c');
});
// describe outer-a
// describe inner 1
// describe outer-b
// describe inner 2
// describe outer-c
// test 1
// test 2
// test 3
Just like the describe
and test
blocks Jest calls the before*
and after*
hooks in the order of declaration. Note that the after*
hooks of the enclosing scope are called first. For example, here is how you can set up and tear down resources which depend on each other:
note
If you are using jasmine2
test runner, take into account that it calls the after*
hooks in the reverse order of declaration. To have identical output, the above example should be altered like this:
beforeEach(() => console.log('connection setup'));
+ afterEach(() => console.log('connection teardown'));
beforeEach(() => console.log('database setup'));
+ afterEach(() => console.log('database teardown'));
- afterEach(() => console.log('database teardown'));
- afterEach(() => console.log('connection teardown'));
// ...
If 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
:
test.only('this will be the only test that runs', () => {
expect(true).toBe(false);
});
test('this test will not run', () => {
expect('A').toBe('A');
});