question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom 'Should' error messages are not dynamic

See original GitHub issue

Current behavior:

it('fails with custom message via overwrite of should', () => {
    cy.wrap(1).should('eq', 1, { message: 'custom error message 1'})
    cy.wrap(1).should('eq', 2, { message: 'custom error message 2'})
  })

Actual error message:

CustomError:  custom error message 1

Expected error Message:

CustomError:  custom error message 2

Desired behavior:

Custom error messages should be dynamic and not static.

If a ‘should’ fails, then the custom message of where a ‘should’ failed is expected to be displayed and not the first custom error message on the test run.

Steps to reproduce: (app code and test code)

  /*
    Ref: https://docs.cypress.io/api/cypress-api/custom-commands.html#Overwrite-Existing-Commands
    Add this to /cypress/support/commands.js
  */
  Cypress.Commands.overwrite('should', (originalFn, actual, assertion, expected, options) => {
    if (options && options.message) {
      cy.on('fail', (error, runnable) => {
        error.name = 'CustomError'
        error.message = options.message
        throw error // throw error to have test still fail
      })
    }
    return originalFn(actual, assertion, expected, options)
  })

it('fails with custom message via overwrite of should', () => {
    cy.wrap(1).should('eq', 1, { message: 'custom error message 1'})
    cy.wrap(1).should('eq', 2, { message: 'custom error message 2'})
  })

Versions

v3.8.0

OS Windows 10, browser chrome

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
sainthkhcommented, Dec 30, 2019

@jennifer-shehane It has nothing to do with #3171. Reason below:

@dizhar According to Node.js EventEmitter documentation, on() function calls all event listeners in the ordered of registration. (Internally, cy object inherits methods in node.js EventEmitter).

In your case, you registered your first listener with {message: 'custom error message 1'} with the first should command. And the second should fails a lot of times and it adds a lot of listeners.

Then, when the test fails, it tries to run all the listeners. Then, when it runs the first listener registered by the first should. It throws the first error object (with "custom error message 1".

That’s why it threw the first error message.

Then, how should we solve this problem?

The simplest method is to remove 'fail' listeners before registering new ones like below:

Cypress.Commands.overwrite('should', (originalFn, actual, assertion, expected, options) => {
  if (options && options.message) {
    cy.removeAllListeners('fail') // remove all 'fail' listeners
    // And we're starting from fresh!
    cy.on('fail', (error, runnable) => {
      error.name = 'CustomError'
      error.message = options.message
      throw error // throw error to have test still fail
    })
  }

  return originalFn(actual, assertion, expected, options)
})

But if you created multiple custom commands and they use 'fail' event, they can cause flaky tests. In that case, you need to remove listeners when the should ran successfully like below:

Cypress.Commands.overwrite('should', (originalFn, actual, assertion, expected, options) => {
  if (options && options.message) {
    const listener = (error, runnable) => {
      error.name = 'CustomError'
      error.message = options.message
      throw error // throw error to have test still fail
    }

    const removeListener = () => {
      cy.removeListener('fail', listener)
      cy.removeListener('command:end', removeListener)
    }

    cy.on('fail', listener)
    cy.on('command:end', removeListener)
  }

  return originalFn(actual, assertion, expected, options)
})

Note: "command:end" event is fired when command is ended. For more information, please check here.

I hope this solved your problem.

0reactions
dizharcommented, Jan 16, 2020

That’s great news! I am looking forward to it.

Also thank you for the quick response.

Best Regards’ Daniel Izhar

On Thu, Jan 9, 2020, 8:16 AM Kukhyeon Heo notifications@github.com wrote:

@dizhar https://github.com/dizhar #6066 https://github.com/cypress-io/cypress/pull/6066 is merged and everything will be fixed in the next version of Cypress.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cypress-io/cypress/issues/6060?email_source=notifications&email_token=ABTIQA5X3FMTVO73TOQ4HV3Q426LBA5CNFSM4KAJMUPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIPD6GQ#issuecomment-572407578, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTIQA3AKTH66CATLU7EVETQ426LBANCNFSM4KAJMUPA .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating A Custom Dynamic Error / Warning Message
The customer is looking for a way to show the user a warning message or a normal message that they can customize dynamically...
Read more >
Custom Error Messages - express-validator
You can build dynamic validation messages by providing functions anywhere a validation message is supported. This is specially useful if you use a...
Read more >
Dynamic error message for custom validator clientside
In your validation javascript you can change the message by accessing it via the source : source.errormessage = "custom message here";.
Read more >
Displaying Dynamic Error Message in ASP.NET Custom ...
This article describes how to display a dynamic error message in an ASP.NET custom validator control.
Read more >
Dynamic error message - Forums - IBM Support
You will be able to read questions and answers, but not make new posts, ... Could you please help me out how to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found