Setting Up and Tearing Down Tests in Jest

8 min read·Jan 1, 2025

In Jest, it will sometimes happen that some of the tests you write require initial setup before their execution and teardown afterwards, such as inserting and removing a set of database records.

One-time setup

To only perform a setup once, before and/or after all the test cases, you can declare the beforeAll() and afterAll() global functions at the very top of your test file, before all the test cases:

beforeAll(() => {
  // setup
});

afterAll(() => {
  // setup
});

// tests

Note: The beforeAll() and afterAll() functions can also handle asynchronous code the same way as tests do, which means that they must either return a promise or execute the done() handler.

Example

In this example, the beforeAll() and afterAll() hooks will be executed only once before and after all test cases:

beforeAll(() => console.log('before_all_tests'));
afterAll(() => console.log('after_all_tests'));

it('', () => console.log('test_1'));
it('', () => console.log('test_2'));

Which will produce a similar output:

before_all_tests
test_1
test_2
after_all_tests

Repeating setup

To perform a setup once, before and/or after each test case, you can declare the beforeEach() and afterEach() global functions at the very top of your test file, before all the test cases:

beforeEach(() => {
  // setup
});

afterEach(() => {
  // setup
});

// tests

Example

In this example, the beforeEach() and afterEach() hooks will be executed before and after each test cases:

beforeEach(() => console.log('before_each_test'));
afterEach(() => console.log('after_each_test'));

it('', () => console.log('test_1'));
it('', () => console.log('test_2'));

Which will produce a similar output:

before_each_test
test_1
after_each_test

before_each_test
test_2
after_each_test

Scoping

When declared within a describe() block, the before*() and after*() functions will only apply to the tests specified in that block.

However, it's important to note that the top-level beforeEach() function will always be executed before the one specified in the describe() block.

Example

In this example, the beforeEach() and afterEach() hooks within the describe() block will only be executed before and after the second test case:

beforeAll(() => console.log('before_all_tests'));
afterAll(() => console.log('after_all_tests'));

beforeEach(() => console.log('before_each_test_1'));
afterEach(() => console.log('after_each_test_1'));

it('', () => console.log('test_1'));

describe('', () => {
  beforeEach(() => console.log('before_each_test_2'));
  afterEach(() => console.log('after_each_test_2'));

  it('', () => console.log('test_2'));
});

Which will produce a similar output:

before_all_tests

before_each_test_1
test_1
after_each_test_1

before_each_test_1
before_each_test_2
test_2
after_each_test_2
after_each_test_1

after_all_tests

Summary

Here's a summary of what you've learned in this lesson:

  • The beforeAll() hook is used to execute setup code once before all tests.
  • The afterAll() hook is used to execute teardown code once after all tests.
  • The beforeEach() hook is used to execute setup code before every single test.
  • The afterEach() hook is used to execute teardown code after every single test.

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
Setting Up and Tearing Down Tests in Jest | Backend Brewery