Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex not working #2417

Open
2 tasks done
BrunoQuaresma opened this issue Nov 16, 2022 · 2 comments
Open
2 tasks done

Regex not working #2417

BrunoQuaresma opened this issue Nov 16, 2022 · 2 comments
Labels
support General questions or support.

Comments

@BrunoQuaresma
Copy link

BrunoQuaresma commented Nov 16, 2022

Please avoid duplicates

Reproducible test case

https://runkit.com/brunoquaresma/63755661db9fc7000800fa7e

Nock Version

13.2.9

Node Version

14.21.1

TypeScript Version

No response

What happened?

I'm trying to use regex to match the path but it is not working:

const nock921 = require('nock@13.2.9')
nock921("http://localhost/api/v2", {allowUnmocked: true})
  .get(new RegExp("^/users/first"))
  .reply(200, 'Match regex');

try {
    await got('http://localhost/api/v2/users/first');
    console.log('nock@9.2.1 is ok')
} catch (error) {
    console.log('nock@9.2.1 is not ok')
}

Would you be interested in contributing a fix?

  • yes
@sospedra
Copy link

sospedra commented Dec 21, 2022

Exactly same problem in v13.2.9
I had to define my RegExp with a loose start
image
Then it's working

@mikicho
Copy link
Contributor

mikicho commented Feb 16, 2024

It seems like nock function should specifiy only the hostname and not the whole path.
e.g. this is working:

  it.only('post with another regexp as spec', async () => {
    const scope = nock('http://localhost')
      .get(new RegExp("^/api/v2/users/first"))
      .reply(200, 'Match regex');

    const { statusCode } = await got.get('http://localhost/api/v2/users/first')

    expect(statusCode).to.equal(200)
    scope.done()
  })

Can you specifiy the path in the get/post/etc function instead?

@gr2m gr2m added support General questions or support. and removed bug labels Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support General questions or support.
Projects
None yet
Development

No branches or pull requests

4 participants