Search code examples
regexcypresscontains

How to verify a result is in a GUID format in Cypress test?


In my Cypress tests, I need to verify that a value is in a GUID format.

Here's an example of the value returned: fbb4f73c-0e3b-4fda-ad0a-81a1b8a8c72f

I've tried asserting using a RegEx below:

resultString = Regex.Replace(subjectString,
            "(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$",
            "'$0'");

        expect(myXhr.response.body.Id).should('contain', /resultString/)

But I get the following error message:

Invalid Chai property: should


Solution

  • You have to use .match to compare your value against a RegEx. You can check the Cypress Assertions Page for all the assertions that cypress supports.

    expect(myXhr.response.body.Id).to.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i)