Cypress string comparison failing

1.5k views Asked by At

The strings are exactly the same. Why does Cypress think they are different?

const resultsMessage = `Search results for ${testQuery}`;

cy.get('[data-test=results-header]').then((el) => {
  expect(el.text().trim()).to.contain(resultsMessage);
});
expected 'Search results for featured calc when logged out' 
to include 'Search results for featured calc when logged out'
1

There are 1 answers

0
Frank On

Maybe a bit of a late answer but I ran into the same problem today. I suspect that the html text you are comparing contains one or more &nbsp. Is this the case you can replace them as follows:

const resultsMessage = `Search results for ${testQuery}`;

cy.get('[data-test=results-header]').invoke('text').then((text) => {
// replace the space char
expect(text.replace(/\u00a0/g, ' ')).equal(resultsMessage)
})

see: https://github.com/cypress-io/cypress/issues/9531