HyeEun
HyeEun

Reputation: 707

Can I have multiple 'it' in one 'describe' and use a beforeEach

So, I have a beforeEach to load up my site. I am mainly writing my test in one describe function with one it function.

my beforeEach is in the index.js>Support>Cypress folder

beforeEach(() => {
  cy.visit('http://localhost:3000/');
});

Current Code Example:

describe('this is a test',function(){
    it('will be a test example',function(){
        cy.contains('test').click
        cy.contains('another test').click()
    })
})

A co worker pointed out that I should try to break them up to be more clear on what each test is doing so long as each part can run independently. I have several tests that can run independent, however, the beforeEach kicks in for each it('test_name',function() I write.

For example: I write a test to open a card, click on a state, add data about that state, then close. If I break each part into it's own it then it will have the beforeEach start back on the "home" page of my site.

Example of desired code:

describe('this is a test',function(){
    it('will be a test example',function(){
        cy.contains('test').click
    })
    it('will test another test',function(){
        cy.contains('another test').click()
    })
})

Is there a way for those several it functions to continue on the previous test rather than having the beforeEach effect it?

Thank you in advance.

Upvotes: 1

Views: 3190

Answers (1)

Mr. J.
Mr. J.

Reputation: 3741

You can use beforeEach() and before(). The behaviour of both is slightly different. before() only applies once per describe() beforeEach()applies for every it() in the describe()

Thus what you have to do: Put all steps you want to only perform once per describe()in a before(). If there are still steps left which you want to perform every it(), put those in a beforeEach().

Note that you can use both before() and beforeEach() together.

Upvotes: 1

Related Questions