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
Mocked request starts timing out after picking up v10.0.5 #1334
Comments
Hi! Sorry about the difficulty and thanks for opening an issue. Do you mean an example of using fake timer? Sinon's fake timers? I'm not familiar with using fake timers with nock. Could you create an example? Adding a test would be really helpful. It would document the behavior and prevent a future regression. Your help with that would be really welcome :) |
I'm also encountering this issue. Here is a simple example of it occurring: https://github.com/humantree/nock-fake-timers This test passes on 10.0.4, but fails on 10.0.5. |
@gr2m @taylor1791 would appreciate your opinions on this issue since the change was introduced by you. I wonder should the behaviour of this library be affected by using fake timer. Because the real http request does not seem to be affected by timer (It's managed in a different phase of the node js event loop.).
https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/ |
Regarding the statement:
I completely agree, but I think that I think that should be discussed in a different MR. In addition, it is certainly a breaking change if !677 is. If the maintainers of nock deem it appropriate, I am happy to help contribute a fix. I think there are a few choices to fix it, but I agree with @chenghung. I think the best "fix" is to revert, mark it as a breaking change, and re-publish this change in v11. However, I can think of a few reasons the maintainers might not desire this. I will defer to the maintainers if they think we should fix this. I can see it 2 ways.
First, assume that all assertion frameworks using fake timers equally. If we knew what percent of javascript projects that use sinon, also use fake timers, we could support or reject 2. I would say that if >4% of projects use fake timers, then view 2 is supported. Others are welcome to submit different percentages. I tried to collect some data using open source projects, but it seemed inconclusive to me. If you are interested in the inconclusive results keep reading. Using Searching for If we assume that jest users use mock timers about the same as mocha users, we can another data point. Using It is possible that projects using any testing framework, sinon, and fake timers do not break (for various reasons). However, this only increases makes the reported percentages higher upper bounds. If we want to go further, someone could try to clone a fraction of repositories and try to find failures after upgrade nock, but that is fraught with perils. Looking at the quality of the search result, it is unclear which one is "closer" to reality. Perhaps we could use activity on this issue as a gauge for the number of projects updating that broke, but I am not sure how to gage total projects that upgraded without problems. To go further, one try to clone a group of repos |
@taylor1791 you made good points there. We'll see how maintainers respond. Also worth pointing out, not all repos are on the public github, there are also ones on other souce code management systems. |
I can see the argument that this should be a breaking change. It begs the question though: is the new behavior desirable? @humantree thanks for the example: const axios = require('axios');
const { expect } = require('chai');
const nock = require('nock');
const sinon = require('sinon');
describe('Test', () => {
const url = 'http://www.google.com';
let clock;
before(() => clock = sinon.useFakeTimers());
after(() => clock.restore());
beforeEach(() => nock(url).get('/').reply(201));
afterEach(() => expect(nock.isDone()).to.equal(true));
it('should pass this test', async () => {
const res = await axios(url);
expect(res.status).to.equal(201);
});
}); How would you take advantage of fake timers in a test with nock? @taylor1791 do you have an example where fake-timing nock's internal timer was desirable? |
In my case, the test that broke was testing a library that hit a list of URLs multiple times a second. In order to prevent this, I was using Sinon to freeze the timer so that I could use Nock to fake these URLs and be sure they were only hit once. |
@paulmelnikow I'd like to address that if you only use fake timer without using I'd encourage whatever new capability introduced by this change should be exposed as a nock option instead of exposing the internal implementation. it should at least be controlled by nock directly, e.g. However, the handling of https responses is not affected by timers at all in node.js . This change can misguide the users of |
@paulmelnikow @taylor1791 @humantree To make sure we don't go too far away from the original issue. Can we work on fixing this first by reverting the change if we all agree on the following points:
A new issue should be raised against this repo to figure out the use case of this |
I'm not yet sure if this is a breaking change? The behaviour that broke for you was never documented or tested. But as this affects so many people I’d be okay with reverting it for now. We can release it as a breaking change, but what would the migration path be? I use lolex whenever I need to mock time and never had problems with it when using the |
@gr2m |
Thanks @Chengxuan. Could someone send a PR to revert #677? We will release it as a fix release |
It'd be helpful to include a test, as well, that |
I'll work on a PR then. |
🎉 This issue has been resolved in version 10.0.6 🎉 The release is available on: Your semantic-release bot 📦🚀 |
🎉 This issue has been resolved in version 11.0.0-beta.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue and add a reference to this one if it’s related. Thank you! |
🎉 This issue has been resolved in version 11.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
What is the expected behavior?
A minor version update of
nock
library shouldn't impact my existing tests.What is the actual behavior?
All my tests uses
fakeTimers
are now timing out.Possible solution
Revert the fix for #677, or make it optional if it has to go as a minor version update.
How to reproduce the issue
Have
sinon.useFakeTimers()
in the test case.Having problem producing a test case? Try and ask the community for help. If the test case cannot be reproduced, the Nock community might not be able to help you.
Does the bug have a test case?
Versions
@gr2m @taylor1791
The text was updated successfully, but these errors were encountered: