title: Cypress: Why finicky in handling spec's after hook?
body: <p>Blocked by a Cypress custom command not executing properly within a <strong>spec's after hook</strong> when a test failed. Experimented with the following minimal spec examples.</p>
  <p><strong>Context</strong></p>
  <p>Testing two Cypress custom commands:</p>
  <ul>
  <li><code>cy.cmdLogin()</code>: Standard username password authentication</li>
  <li><code>cy.cmdLogout()</code>: <code>cy.get('logout_button').click()</code></li>
  </ul>
  <p><strong>Spec #1, <code>cy.cmdLogout()</code> in after hook does work</strong></p>
  <p>Very simple Cypress tests spec, test <code>cy.cmdLogin()</code> with after hook with <code>cy.cmdLogout()</code>, works perfectly:</p>
  <pre><code> context('SPEC Login', () =&gt; {
      after('AFTER Logout', () =&gt; {
        if (this.successLogin) {
          cy.cmdLogout().then(() =&gt; {
            cy.log('Logout Success');
          });
        }
      });
  
      it('TEST Login', () =&gt; {
        cy.cmdLogin().then(() =&gt; {
          cy.log('Login Success');
          this.successLogin = true;
        });
      });
    });
  </code></pre>
  <p><strong>Spec #2, <code>cy.cmdLogout()</code> in after hook does not work</strong></p>
  <p>Modified Cypress tests spec, test <code>cy.cmdLogin()</code> with after hook with <code>cy.cmdLogout()</code>, now added a test that forced failure <code>expect(false).to.be.a('boolean').to.be.true;</code>. The after hook <code>cy.cmdLogout()</code> is called and fails:</p>
  <p>Verified that Cypress could handle <code>cy.get('logout_button')</code> without failure.</p>
  <p>Yet, extending with <code>.click()</code>, <code>cy.get('logout_button').click()</code>, Cypress would throw an error: Can not perform <code>click()</code> on <code>undefined</code>.</p>
  <pre><code> context('SPEC Login', () =&gt; {
      after('AFTER Logout', () =&gt; {
        if (this.successLogin) {
          cy.cmdLogout().then(() =&gt; {
            cy.log('Logout Success');
          });
        }
      });
  
      it('TEST Login', () =&gt; {
        cy.cmdLogin().then(() =&gt; {
          cy.log('Login Success');
          this.successLogin = true;
        });
      });
  
      it('TEST Fail', () =&gt; {
        expect(false).to.be.a('boolean').to.be.true;
      });
    });
  </code></pre>
  <p><strong>Spec #3, removed after hook, <code>cy.cmdLogout()</code> moved to another test does work</strong></p>
  <p>Modified Cypress tests spec again and removed after hook, test <code>cy.cmdLogin()</code>, test forced failure, and now test <code>cy.cmdLogout()</code>. This works perfectly:</p>
  <pre><code>  context('SPEC Login', () =&gt; {
      it('TEST Login', () =&gt; {
        cy.cmdLogin().then(() =&gt; {
          cy.log('Login Success');
          this.successLogin = true;
        });
      });
  
      it('TEST Fail', () =&gt; {
        expect(false).to.be.a('boolean').to.be.true;
      });
  
      it('TEST Logout', () =&gt; {
        if (this.successLogin) {
          cy.cmdLogout().then(()=&gt; {
            cy.task('log', 'Logout Success');
          });
        }
      });
    });
  </code></pre>
  <p><strong>Insight as to why Cypress is finicky with spec's after hook would be very much appreciated</strong></p>