Nock affects other test cases with different URLs
See original GitHub issueWhat is the expected behavior? Nock should differentiate URL parameters.
What is the actual behavior? Nock does not differentiate URL params and affects other test cases.
Possible solution
Does the bug have a test case?
I have two separate it
cases included in a single describe
. The first tests is for a case when everything goes fine:
const ips = ['1.2.3.4', '11.12.13.14'];
it('should return successfully resolved geolocation', async () => {
const cacheKey = `${ips[0]}_${locale}`;
nock('https://www.travelpayouts.com')
.get('/whereami')
.query({ locale, ip: ips[0] })
.reply(200, exampleResult);
redisGetMock.mockResolvedValueOnce(null);
redisSetMock.mockResolvedValueOnce('OK');
const result = await getUserGeo(ips[0]);
expect(redisGetMock).toHaveBeenCalledWith(cacheKey);
expect(redisSetMock).toHaveBeenCalledWith(
cacheKey,
JSON.stringify(exampleResult),
'EX',
864000, // 10 days
);
expect(result).toStrictEqual(exampleResult);
});
And then I have a test case for a failing version of my function:
it('should fallback to default response body on got error', async () => {
const cacheKey = `${ips[1]}_${locale}`;
nock('https://www.travelpayouts.com')
.get('/whereami')
.query({ locale, ip: ips[1] })
.reply(500, 'Internal Server Error');
redisGetMock.mockResolvedValueOnce(null);
const result = await getUserGeo(ips[1]);
expect(redisGetMock).toHaveBeenCalledWith(cacheKey);
expect(redisSetMock).not.toHaveBeenCalled();
expect(result).toStrictEqual({
coordinates: '37.617633:55.755786',
country_name: 'Россия',
iata: 'MOW',
name: 'Москва',
});
});
The problem here is that the second test will always fail because sindresorhus/got returns 200 OK instead of 500 Internal Server no matter what. So why is this happens? Because the URLs are different:
- https://www.travelpayouts.com/whereami?locale=ru&ip=1.2.3.4
- https://www.travelpayouts.com/whereami?locale=ru&ip=11.12.13.14
But if I put failing test before a regular one then everything goes just fine and all tests are passing. If I remove nock config with 500 error then test will also fail and request will match for nock 200 config.
Versions
Software | Version(s) |
---|---|
Nock | 13.0.5 |
Node | 12.18.4 |
TypeScript | 4.0.3 |
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)
This is the part that is causing to me scratch my head.
Couple of questions:
Are you aware that Got retries failed requests by default? Are you turning off the retry?
Do you disabling all real traffic before your tests start to ensure other requests aren’t happening?
Have you checked the debug output to validate the requests you’re expecting to see and what they’re matched against?
@mastermatt I’m not familiar with reading nock debug logs but are three requests made? I only see two
... attempting match ...
andmatching X to GET Y
lines.I notice that for the 2nd request, there are these lines:
why does the
nock.socket socket destroy
twice, and why is there at first 1 interceptor, then 0 interceptors?edit I’ve noticed that if the request times out, it can cause a premature
nock.socket socket destroy
. If the request client retries the request, there will be 0 interceptors because the first was already used up. However I don’t know if that’s your issue.