1

I am trying to match URLs.

lab.before(async () => {
  nock('https://dev.azure.com')
    .get(centosAzureUri)
    .times(5)
    .reply(201, [
    ...

If I use a string, it is working just fine. An example is below: const centosAzureUri = `/${conf.org}/${conf.buildProject}/_apis/build/builds?api-version=4.1&branchName=${conf.buildBranch}`

However, I want to use a RegEx as below: const centosAzureUri = new RegExp(`/${conf.org}/${conf.buildProject}/_apis/build/builds?api-version=4.1.*`, 'g') That is not working.

According to the documentation, nock should accept regular expressions and .* should match any symbol [because of the .] and allow those matched characters to be repeated any number of times. Hence, I am assuming this should accept any string ending, including &branchName=${conf.buildBranch}.

What I am doing wrong?

Moshe Shmukler
  • 1,270
  • 2
  • 21
  • 43

2 Answers2

2

I think nock only uses regex literal vs. regex object which will return a new object. eg.

  nock('http://example.com')
       .get(/harry\/[^\/]+$/)
       .query({param1: 'value'})
       .reply(200, "OK");

See related How to build nock regex for dynamic urls

4m1r
  • 12,234
  • 9
  • 46
  • 58
  • Thank you. Literals are working. Personally, I did not see anything either written nor in the `nock` code that would prevent RegEx objects from working. In the meantime, followed your suggestion. – Moshe Shmukler Oct 26 '18 at 09:40
1

Please note that RegExp only needs the pattern up to "4.1" to perform a match. The rest of the string will be ignored if the match occurs. For example:

const centosAzureUri = new RegExp(`/${conf.org}/${conf.buildProject}/_apis/build/builds?api-version=4.1`, 'g')

Further, you may want to try escapements, since slashes require those:

const centosAzureUri = new RegExp(`\/${conf.org}\/${conf.buildProject}\/_apis\/build\/builds?api-version=4.1`, 'g')

HTH!

Doug Ross
  • 48
  • 6