title: Cypress.io How to handle async code
body: <p>I'm in the middle of process of moving our old capybara tests to cypress.io as our application is going SPA way.</p>
  
  <p>In our case we have over 2000 tests covering a lot of features.
  So common pattern to test feature is to have an user with created and published offer. </p>
  
  <p>On the beginning I wrote case where cypress were going trough page and clicking everything. It worked but I saw that offer create + publish took almost 1,5 minute to finish. And sometimes we need multiple offers. So we have a test which takes 5 minutes and we have 1999 left to rewrite.</p>
  
  <p>We came up with REST API to create offer and user, basically shortcut for test env preparation.</p>
  
  <p>I came to the point where everything is working using <code>async/await</code>. So here's the thing. If I want to use normal async JS code with cypress I get <code>Error: Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.</code></p>
  
  <p>Here's how it looks like:</p>
  
  <pre><code>    const faker = require('faker')
      import User from '../../support/User';
  
      describe('Toggle button for description offer', () =&gt; {
        const user = new User({
          first_name: faker.name.firstName(),
          last_name: faker.name.firstName(),
          email: `QA_${faker.internet.email()}`,
          password: 'xxx'
        })
        let offer = null
  
        before(async () =&gt; {
          await user.createOnServer()
          offer = await user.createOffer()
          await offer.publish()
        })
  
        beforeEach(() =&gt; {
          user.login()
          cy.visit(`/offers/${offer.details.id}`)
          cy.get('.offer-description__content button').as('showMoreButton')
        })
  
        it('XXX', function () {
          ...some test
        })
      })
  </code></pre>
  
  <p>This snippet works as expected. Firstly it fires before and creates whole env then when it's done it goes further to beforeEach and starts testing.</p>
  
  <p>Now I would like to merge before and beforeEach like</p>
  
  <pre><code>  before(async () =&gt; {
      await user.createOnServer()
      offer = await user.createOffer()
      await offer.publish()
      user.login()
      cy.visit(`/offers/${offer.details.id}`)
      cy.get('.offer-description__content button').as('showMoreButton')
    })
  </code></pre>
  
  <p>Which will fail because of async keyword.
  Now the question is: how to rewrite it to use async/await and cypress commands together? I tried to rewrite it with normal Promise but It won't work too ...</p>
  
  <p>Any help appreciated.</p>