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. Pest provides helper functions to handle this.

# Runs before each test on this filebeforeEach(function () {Database::migrate();});# Runs after each test on this fileafterEach(function () {Database::delete();});test('city database has Vienna', function () { assertTrue(City::exists('Vienna'));});test('city database has San Juan', function () { assertTrue(City::exists('San Juan'));});

One-Time Setup

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 just do it inline. Pest provides beforeAll and afterAll to handle this situation.

# Runs before the first test of the filebeforeAll(function () {Database::migrate();});# Runs after the last test of the fileafterAll(function () {Database::delete();});test('city database has Vienna', function () { assertTrue(City::exists('Vienna'));});test('city database has San Juan', function () { assertTrue(City::exists('San Juan'));});