From the course: End-to-End JavaScript Testing with Cypress.io

Unlock this course with a free trial

Join today to access over 24,100 courses taught by industry experts.

Using beforeEach

Using beforeEach

- [Instructor] All right, so now that we've taken a look at a few details around writing simple tests with Cypress, the next thing that I want to show you is a key way that we can reduce duplication across our Cypress tests. So first of all, let's just create a new file here. We'll call this something like before each. You'll see why I'm calling it this in just a minute. Then what we're going to do is we're just going to go into our selecting.cy.js file. I'm just going to copy the code that's inside there and paste it and we'll just adjust the indentation, real quick here. All right, there we go. So let's say that we made this test a little bit easier to read and maintain by splitting this single it block up into multiple blocks. You'll see what I mean by this in just a second. Let's maybe copy and paste this a few times so that we now have three tests here. And maybe what we want to do is for each of these it blocks, instead of typing incrementally, which can be a little bit…

Contents