Mr. J.
Mr. J.

Reputation: 3731

How to let Cypress return custom errors or messages

I'm looking for a way to create a custom message instead of the standard messages. What I have is this piece of code (scrambled a bit):

Cypress.Commands.add('authenticate', {prevSubject: 'optional'}, (_, fixture) => {
  return cy.fixture(fixture).then(credentials => {
    cy
      .apiRequest('scrambled/url/here', {
        method: 'post',
        authorizationMethod: 'Basic',
        token: apiOauthSecret,
        data: credentials
      })
      .then(resp => {
        expect(resp.status)
          .to
          .eq(201, 'Unable to authenticate, login failed')
        storeTokenInSession(resp.body)
      })
  })
})

That code results in a error like this:

If I fix the code so the result is okay the result looks like this:

As you see the message should be displayed if the eq() fails instead of when the eq() succeeds.

I want to show the message only if the check fails, but I also want the test to break and stop.

Do you people have any idea how to get this working? I already looked into using cy.log() and the solution shown here.

Upvotes: 11

Views: 15730

Answers (3)

Gus Murphy
Gus Murphy

Reputation: 336

What I've done in this case is to simply throw an error. To me it looks like the sample code in the question is abusing expect, based on the assumption that you're not actually writing a test case here, but a function to use in the course of a test. Here is my proposed solution:

Cypress.Commands.add('authenticate', {prevSubject: 'optional'}, (_, fixture) => {
  return cy.fixture(fixture).then(credentials => {
    cy
      .apiRequest('scrambled/url/here', {
        method: 'post',
        authorizationMethod: 'Basic',
        token: apiOauthSecret,
        data: credentials
      })
      .then(resp => {
        if (resp.status !== 201) {
          throw new Error('Unable to authenticate, login failed')
        }

        storeTokenInSession(resp.body)
      })
  })
})

This would cause a test that uses that command to fail if this authenticate function did not get a 201 response, and you would get (what I would consider to be) a helpful message in the Cypress UI.

I feel like a root problem here and in some of the other responses is that the testing functionality (expect, again) is being abused. While in some sense you are "testing" the response status, what I believe is trying to be achieved here is not a "test" in the way that Cypress is concerned.

Upvotes: 0

Benur21
Benur21

Reputation: 177

No need for Cypress.log().

if (resp.status !== 201) {
  expect(resp.status, 'Unable to authenticate, login failed').to.eq(201)
}

If it's good, it moves on. Else it fails with the error message.

An advantage of this is you don't get a grey error message, you get it in red instead.

If you also want the success case just add:

else {
  expect(resp.status, 'Able to authenticate, login succeeded').to.eq(201)
}

Upvotes: 6

kuceb
kuceb

Reputation: 18043

You can use Cypress.log for that:

if (resp.status !== 201) {
  Cypress.log({
    name: 'Authentication',
    message: 'failed'
  })
}
expect(resp).its('status').eq(201)

Upvotes: 4

Related Questions