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.

Cypress 'expect' with 'regex' fails to validate on second occurrence.

See original GitHub issue

Current behavior

Steps to reproduce:

  1. Read a text from any web page.
  2. Use expect to match a regex.
  3. Now, read the same data from the web page again.
  4. Use expect to match a regex.

Actual result: Validation passes the first time but the same validation fails the second time.

Cypress Expect Failure

Desired behavior

The validation should pass each time if the condition is satisfied or report the appropriate failure if the condition fails.

Test code to reproduce

/* eslint-disable cypress/no-unnecessary-waiting */
/* eslint-disable no-unused-vars */
/// <reference types="Cypress" />

it("Testing Table Validation", () => {
    let regex = /Germany|UK/gm;
    cy.visit("https://www.w3schools.com/html/html_tables.asp");
    cy.get("table[id='customers'] tbody tr:nth-child(2) td:nth-child(3)")
      .then(element => {
          cy.log(element.text());
          expect(element.text()).to.match(regex); // This validation works fine.
      });

    cy.wait(2000);
    cy.get("table[id='customers'] tbody tr:nth-child(2) td:nth-child(3)")
      .then(element => {
          cy.log(element.text());
          expect(element.text()).to.match(regex); // This validation fails.
      });
});

Cypress Version

8.5.0

Other

Am I doing something wrong here? The same scenario works if I use should instead of expect.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
chrisbreidingcommented, Dec 1, 2021

I’m going to close this as there’s a solution and I don’t think we should interfere in the behavior of regular expressions for this case. It’s really more of an issue with JavaScript itself and not Cypress, similar to needing to do a deep comparison for two object references (e.g. expect({}).to.equal({}) vs expect({}).to.deep.equal({})).

0reactions
praveentatacommented, Dec 1, 2021

Thanks Chris. That makes it clear.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress - use of regular expression in 'contains' function ...
contains(/^Tier2_Forest/) fails because the text is not at the beginning of the string, there is white space preceding it. Just try .contains(/ ...
Read more >
Assertions | Cypress Documentation
Cypress bundles the popular Chai assertion library, as well as helpful extensions for Sinon and jQuery, bringing you dozens of powerful assertions for....
Read more >
Assertions | Cypress examples (v9.4.1) - Gleb Bahmutov
Match numbers using a regular expression. Imagine we want to validate the date inside an element. The date should have the day number...
Read more >
What are Cypress Assertions and How to use ... - Tools QA
Otherwise, it will not be feasible to validate whether the application reached the expected state or not. Cypress itself bundles assertions ...
Read more >
How to handle Cypress Assertions : Tutorial - BrowserStack
For every test, it is essential to have a validation that checks whether it functions as expected or not. Assertions are these validations ......
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