Cypress: Multiple 'it' in one 'describe' with a beforeEach

Created on 10 May 2019  路  4Comments  路  Source: cypress-io/cypress

Can I have multiple 'it' in one describe' but also be running a beforeEach?

Current behavior:

Currently, I am running a beforeEach and it resets at the beginning of each test which is what I want but also no. I have tests that are independent but grouped together because they are testing the same component. So, for now it looks like this:

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

And my beforeEach looks like this:

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

Desired behavior:

I want to be able to write my tests like this:

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()
    })
})

Summary

So is there anyway to not have the beforeEach effect that second it in my desired behavior? or will I have to write a cy.visit() in each test?

question

Most helpful comment

You _can_ have multiple it within the scope of a single describe. You can honestly keep nesting layers of describe and it as freely as you'd like, as long as there are no describe nested within an it.

The real issue seems to be your summary...

Your beforeEach will run before every single it and describe within its own describe's scope.

If you have a test that you dont want affected by a describe's beforeEach, consider making a new describe, outside of the other's scope... like so:

describe('test suite', () => {
  beforeEach(() => {});

  it('test 1', () => {});

  it('test 2', () => {});
});

describe('test suite 2', () => {
  beforeEach(() => {});

  it('test 1', () => {});

  it('test 2', () => {});
});

All 4 comments

You _can_ have multiple it within the scope of a single describe. You can honestly keep nesting layers of describe and it as freely as you'd like, as long as there are no describe nested within an it.

The real issue seems to be your summary...

Your beforeEach will run before every single it and describe within its own describe's scope.

If you have a test that you dont want affected by a describe's beforeEach, consider making a new describe, outside of the other's scope... like so:

describe('test suite', () => {
  beforeEach(() => {});

  it('test 1', () => {});

  it('test 2', () => {});
});

describe('test suite 2', () => {
  beforeEach(() => {});

  it('test 1', () => {});

  it('test 2', () => {});
});

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

Is that why it's resetting every time because it's not written the way you wrote in your example?

If you have a beforeEach inside cypress/support/index.js, that beforeEach will get called on every single one of your tests, in every suite, in every integration file.

https://docs.cypress.io/guides/references/best-practices.html#State-reset-should-go-before-each-test

I gotcha. Thanks for the answer; I will most likely just re-write the code to include the beforeEach in every test EXCEPT the one and just use the regular cy.visit() so that I can have multiple it functions.

Was this page helpful?
0 / 5 - 0 ratings