Cypress Test: How to Use Before, After, BeforeEach, and AfterEach Hooks

Krisnawan Hartanto
Software Testing Pipeline
4 min readDec 12, 2022

This article is a part of the Cypress Test series. To read another story in this series, please follow the links below:

Cypress Test Series:

In the context of the Cypress testing framework, a “hook” is a piece of code that runs before or after a test or group of tests. This can be useful for performing setup and cleanup tasks, such as starting and stopping a server, or loading and clearing data from a database.

Photo by Mael BALLAND on Unsplash

Cypress provides several different types of hooks, including before and after hooks that run before or after a single test, and beforeEach and afterEach hooks that run before or after each test in a group of tests. Hooks can be defined at the global level, or within a specific described block to apply only to the tests within that block.

Syntax

Here is an example syntax of how you might use a beforeEach hook to load data into a database before each test in a group of tests:

describe(‘My Tests’, () => {
beforeEach(() => {
// load data into database
});

it(‘should do something’, () => {
// test goes here
});

it(‘should do something else’, () => {
// test goes here
});
});

In this example, the code within the beforeEach hook will run before each of the two tests, ensuring that the data is loaded into the database before each test is run. This can help to ensure that each test is running in a clean and consistent environment.

Here is an example of before and after hooks that are used to start and stop a server before and after a group of tests:

describe(‘My Tests’, () => {
before(() => {
// start server
startServer();
});

after(() => {
// stop server
stopServer();
});

it(‘should do something’, () => {
// test goes here
});

it(‘should do something else’, () => {
// test goes here
});
});

In this example, the before hook uses a startServer function to start the server before any of the tests in the group are run. The after hook then uses a stopServer function to stop the server after all of the tests have been run. This ensures that the server is only running when the tests are running, which can help to optimize performance and avoid conflicts with other processes that may be using the same port.

Here is an example of a beforeEach hook that loads data into a database before each test in a group of tests:

describe(‘My Tests’, () => {
beforeEach(() => {
// connect to database
const db = connectToDatabase();

// load data into database
loadData(db);
});

it(‘should do something’, () => {
// test goes here
});

it(‘should do something else’, () => {
// test goes here
});
});

In this example, the beforeEach hook establishes a connection to the database and then uses a loadData function to load data into the database. This hook will run before each of the two tests, ensuring that the data is loaded into the database before each test is run.

Additionally, you could define beforeEach and afterEach hooks to perform setup and cleanup tasks before and after each test, like this:

describe(‘My Tests’, () => {
before(() => {
// start server
startServer();
});

after(() => {
// stop server
stopServer();
});

beforeEach(() => {
// connect to database
const db = connectToDatabase();

// load data into database
loadData(db);
});

afterEach(() => {
// clean up database
clearData();
});

it(‘should do something’, () => {
// test goes here
});

it(‘should do something else’, () => {
// test goes here
});
});

In this example, the afterEach hook uses a clearData function to clean up the database after each test is run. This ensures that the database is in a clean state before the next test is run.

Conclusion

In conclusion, Cypress hooks are an important and useful feature of the Cypress testing framework. They allow you to perform setup and cleanup tasks before and after tests, ensuring that each test is run in a clean and consistent environment. By using hooks, you can ensure that your tests are reliable and maintainable, and make it easier to write and organize your tests. Overall, hooks are an essential part of the Cypress testing toolkit, and are an important tool for anyone working with the Cypress framework.

Connect with me

Krisnawan: Twitter | Linkedin | Medium

MydoQA: Twitter | Blog | Instagram | Facebook

--

--

Krisnawan Hartanto
Software Testing Pipeline

Software Development Engineer in Test | Questioning the Unquestionable | Contributor to mydoqa.my.id