question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

option to specify full url: nock(url).get().reply(200)

See original GitHub issue

Feature Request

Allow passing in a complete absolute URL (to be mocked) as a single string. For example:

nock("http://example.com/some/path").get().reply(200);

Context

Currently one has to split the url into hostname and path:

nock("http://example.com").get("/some/path").reply(200);

But sometimes you have a complete URL in your test code (for other reasons) and you just want to mock it as is:

const TEST_URL = "http://example.com/some/path";

I ran into this multiple times and every time I do the following without thinking, just to spend some time figuring out why it does not work (no match for request…):

nock(TEST_URL).get().reply(200);

This would be very intuitive, and nock should do the “hard” work of whatever is necessary internally to mock it.

Alternatives

The only options (afaics) right now are:

  • duplicate the URL (and its components) in your test code => might be difficult, depending on other parts of the tests
  • parse it before passing to nock => tedious

Has the feature been requested before?

I did a quick search on issues but couldn’t find anything (but it’s hard to search for, “url”, “only url” has lots of hits). Sorry if it’s a duplicate!

If the feature request is accepted, would you be willing to submit a PR?

Maybe

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
alexklicommented, Jul 1, 2020

one-to-many relationship between host/scope to path/interceptor

No offense: Having used nock for quite some time, I have (still) no real idea what that sentence means. I believe the internal nock model is not intuitive at all 😉

Would it not be possible to have nock("https://host/basepath") internally simply

  • parse the url
  • if absolute, create or get the existing nock for the host (surely nock has a list of them?)
  • then use the path as base path
  • the returned object would have e.g. a get() that would allow no path (= just base path) or a subpath argument (/basepath/subpath)

I wouldn’t see how this would be a problem… most likely if I am using the absolute variant I have a single URL to nock and don’t care about multiple scopes.

2reactions
Alexseycommented, Apr 23, 2020

The most straight forward way to parse it is:

const {URL} = require('url')
const testUrl = new URL(TEST_URL)

...

nock(testUrl.origin).get(testUrl.pathname).reply(200);

But I agree that it would be nice to do the same inside nock itself

And if it is not going to be supported - there should be an error risen, because now it’s silently ignored and intercept is not working 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to send multiple response using nock if there is no ...
I already have a test case for happy journey in which nock intercepts the request and sends 200 along with some data. nock('http://dummy- ......
Read more >
nock - Devpost
It will intercept an HTTP GET request to '/users/1' and reply with a status 200, and the body will contain a user representation...
Read more >
Untitled
got(url, [options]) Returns a Promise for a `response` object with a `body` property, a `url` property with the request URL or the final...
Read more >
finboxio-nock - npm Package Health Analysis - Snyk
var scope = nock('https://www.example.com') .get('/resource') .reply(200, ... Instead of placing the entire URL, you can specify the query part as an object ...
Read more >
API mock testing with Nock for Node.js apps - LogRocket Blog
This mocking object intercepts a GET request to the specified URL ... as an example — and replies with the status 200 and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found