Search code examples
javascriptjestjsautomated-testsmocha.jswebdriver-io

How to assert a string containing some fixed words along with some variable numbers?


I want to assert: "10 Automated results in 30 days" in which 10 & 30 is coming from API call so it can be anything.

I am looking for Jest assertions, for example:

expect(pageobject.webelement.getText()).toEqual("10 Automated results in 30 days"); // but it fails some other day because 10 & 30 can be any number at any point of time.

How can I assert this such that my code should look for exact string but except any number at specified location?


Solution

  • You can use regex to match the string like this,

    describe('stringMatching', () => {
       const expected = /[0-9]* Automated results in [0-9]* days/;
    
       it('matches if the received value does not match the expected regex', () => {
           expect(pageobject.webelement.getText()).toMatch(expected);
       });
    });