Custom 'Should' error messages are not dynamic
See original GitHub issueCurrent 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:
- Created 4 years ago
- Comments:7 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.jsEventEmitter
).In your case, you registered your first listener with
{message: 'custom error message 1'}
with the firstshould
command. And the secondshould
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: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 theshould
ran successfully like below:Note:
"command:end"
event is fired when command is ended. For more information, please check here.I hope this solved your problem.
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: