cypress

What is the best practice of pass states between tests in Cypress


I want to pass/share data between each test. What is the best way to implement it in Cypress?

For example:

 it('test 1'), () => {
   cy.wrap('one').as('a')
   const state1 = 'stat1'
 })

 it('test 2'), () => {
   cy.wrap('two').as('b')
 })

 it('test 2'), () => {
   //I want to access this.a and this.b

   //Also I want to access state1

 })

Solution

  • In the case of Javascript variables, you can do something like this:

    let state;
    
    describe('test 1', () => {
        it('changes state', () => {
            state = "hi";
         });
    });
    
    describe('test 2', () => {
        it('reports state', () => {
            cy.log(state); // logs "hi" to the Cypress log panel
         });
    });
    

    .as() does not appear to be able to carry state between describe blocks.