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.

Support creating custom assertion operator like 'should('have.text.trimmed', ...)'

See original GitHub issue

Cypress have many assertion operators like ‘eq’, ‘contain’, ‘have.text’, but which is not enough for me.

E.g. I want to check a DOM element have some text but want to skip around whitespaces, I have to write:

cy.get('.cell')
  .then($cell => $cell.text().trim())
  .should('eq', 'Hello')

Since none of have.text, eq, contain really work in this case.

I want to define a custom operator like have.text.trimmed, to simplify the code to:

cy.get('.cell')
  .should('have.text.trimmed', 'Hello')

I can’t find any document about it, does Cypress have a way to do it? Or add this feature?

PS: I also create a question before this issue: https://stackoverflow.com/questions/55842707/how-to-define-a-custom-assertion-operator-in-cypress/55842986

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
bahmutovcommented, Apr 25, 2019

take a look at https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/extending-cypress__chai-assertions - this is how to add new Chair assertions to the existing ones

We could make it simpler I guess by adding something similar to custom commands

Cypress.Assertions.add('...', )
1reaction
freewindcommented, Apr 25, 2019

@jennifer-shehane Thanks for the links and I managed to create it by extending chai. I’ve explained the code on stackoverflow, anyone interested may see it there: https://stackoverflow.com/a/55854585/342235

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I define a custom assertion operator in Cypress?
Define a Chai chainable method trimmed . It allows using .should('have.trimmed.text', 'sometext') , which seems to work, but the assertion is ...
Read more >
Create Custom Assertions For Test Readability - Gleb Bahmutov
The error message explains that the "have.css" assertion expected an element, but instead received "first" subject. What is this about?
Read more >
Creating Custom Assertions - Oracle Help Center
You can develop custom assertions when specific functionality is not provided with the standard policies that come with Oracle Web Services Manager.
Read more >
Assertions | Cypress Documentation
This document is only a reference to every assertion Cypress supports. ... You will commonly use these chainers after using DOM commands like:...
Read more >
Expect - WebdriverIO
When you're writing tests, you often need to check that values meet ... Every matcher can take several options that allows you 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