Search
⌘K
    to navigateEnterto select Escto close

    Using hooks in Cypress

    Hooks are helpful to manage tests in an efficient way. It helps us to set conditions before/after test conditions and also reduce the redundant code.

    before() Hook:

    • before() : If we want to execute some steps only once before all the test cases in a test suite, we can use the before() cypress hook to group all those test steps. It can contain some setup steps that are needed to perform before kicking off the test execution. Please note, this block is not executed again even if the test fails.

    Its syntax looks like below:

    1describe("Test suite", () => {
    2  before(() => {
    3    ...
    4    ...
    5
    6  });
    7});

    beforeEach() Hook:

    • beforeEach() : If we want to execute some steps before each of the test cases in a Test Suite, we can use the beforeEach() block to group all those test steps. It can be something like bringing the application to a specific state before running each test case. Its syntax looks like below:
    1describe("Test suite", () => {
    2  beforeEach(() => {
    3    ...
    4    ...
    5
    6  });
    7});

    afterEach() Hook:

    • afterEach() : If we want to execute some steps after each of the test cases in a test suite. We can use the afterEach() block to group all those test steps. It can contain some teardown steps which we need to perform after finishing the execution of each of the test cases. It can be something like bringing the application or database to a specific state after each test case. Its syntax looks like below:
    1describe("Test suite", () => {
    2  afterEach(() => {
    3    ...
    4    ...
    5
    6  });
    7});

    after() Hook:

    • after() : If we want to execute some steps only once after all the test cases. We can use the after() block to group all those test steps. It can contain some teardown steps that are needed to perform after finishing the execution of all the test cases. Its syntax looks like below:
    1describe("Test suite", () => {
    2  after(() => {
    3    ...
    4    ...
    5
    6  });
    7});
    Previous
    Next