3

In Jest and other test frameworks? What is the point of writing beforeAll?

The following link didn't help:

Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll

beforeAll(() => server.listen());

The line above was placed in the global scope of the codebase I'm examining.

What would change if I rewrote it as follows?

server.listen()

ibrahim koz
  • 537
  • 4
  • 15

1 Answers1

5

When you have asynchronous functions (async/promise) that need to run, without boilerplate code, they may not complete before the tests start, by using before all or after all, you ensure that the async function call resolves before starting.

Additionally, Using a method like beforeAll or afterAll enable one-time setup/teardown functions to be called. If these fail (throw exceptions) then the framework knows that all subsequent tests will fail and to not run them.

You can read more about setup/teardown methods here.

Noah
  • 859
  • 7
  • 17