Search code examples
cypressmailhog

Check email body using cypress mailhog


I'm trying to check if the email body contains, for example, a username, but i get this error saying that the value is undefined

it.only('User info', () => {
  const email= cy.mhGetMailsBySubject('Your account is now  confirmed')
  .mhFirst().mhGetBody().should('contain', 'Thanks for the verification')
  email.should('contain', 'username')

})

its.Body CypressError Timed out retrying after 4000ms: cy.its() errored because your subject is: undefined. You cannot access any properties such as Body on a undefined value.

If you expect your subject to be undefined, then add an assertion such as:

cy.wrap(undefined).should('be.undefined')

Is there a better way to do it?


Solution

  • Don't use the return value of the command, instead chain another assertion like this:

    const username = 'Fred'
    
    cy.mhGetMailsBySubject('Your account is now  confirmed')
      .mhFirst().mhGetBody()
      .should('contain', 'Thanks for the verification')
      .and('contain', username)