Reduce Code Duplication in Cypress Tests with beforeEach

Share this video with your friends

Send Tweet

A lot of our tests will share setup steps. Since Cypress is using Mocha under the hood, we can take advantage of existing features such as the ability to define a beforeEach block that runs before each of our tests. In this lesson we’ll refactor two tests that contain duplicate commands by moving that duplication into the beforeEach for our describe block.

Jacob Evans
Jacob Evans
~ 4 years ago

Can the beforeEach also be done outside the describe block?

Andy Van Slaars
Andy Van Slaars(instructor)
~ 4 years ago

Can the beforeEach also be done outside the describe block?

You can according to the Mocha docs, but I like to keep things grouped in the describe blocks.