1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Trying to assert a date format in Cypress that has white space

Discussão em 'Angular' iniciado por Cholis, Junho 12, 2025.

  1. Cholis

    Cholis Guest

    I am trying to write an assertion in my Cypress test that checks that the date format is mm/dd/yyyy.

    Right now, the assertion is failing and I think it's because there is white space at the start and end of the date, as you can see below.

    To extract the value from the HTML, I used an alias. When I hardcode the date and use contain instead of match it passes, but when I use Regex instead and use match it fails.

    How can I deal with white space when writing an assertion?

    I am testing this using Cypress on a web app built in Angular.

    HTML

    <mat-cell class="mat-mdc-cell mdc-data-table__cell cdk-cell cdk-column-Test-Date mat-column-Test-Date ng-star-inserted" role="cell"> 10/10/2024 </mat-cell>


    Cypress Test

    cy.get("cdk-column-Test-Date").as("testDate");
    cy.get("@testDate").should("match", /^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$/);

    Continue reading...

Compartilhe esta Página