Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to test this regex in jest

I have this one-liner I need to test using jest

export const matchUriRE = /^([^:]*):([^:]*):(.*)$/;

How to test it? Thanks in advance.

like image 792
manuelBetancurt Avatar asked Aug 14 '18 22:08

manuelBetancurt


1 Answers

I know the question is quite old by now and I don't even know if the following solution already existed at the time of asking, but I think the Jest-most way would be:

it('matches URI', () => {
  const uriRegEx = /^([^:]*):([^:]*):(.*)$/;
  const uri = 'http://google.com:4443/'; 
  expect(uri).toMatch(uriRegEx);
});

Potentially also produces a more descriptive error message in case of failure.

For further reference: https://jestjs.io/docs/en/expect#tomatchregexporstring

like image 198
Ricki-BumbleDev Avatar answered Oct 19 '22 17:10

Ricki-BumbleDev