option to specify full url: nock(url).get().reply(200)
See original GitHub issueFeature 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:
- Created 3 years ago
- Reactions:5
- Comments:7 (4 by maintainers)
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 simplyget()
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.
The most straight forward way to parse it is:
But I agree that it would be nice to do the same inside
nock
itselfAnd 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 😦