-
Notifications
You must be signed in to change notification settings - Fork 45
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
Added support for timeout to FakeXMLHttpRequest. #4
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for you work. This seems like a good idea, but I am not sure I follow on why there is a restriction on this functionality to faking time with Sinon? Why can't we use this along with normal timeouts? Other than the tests using more time, I don't see why not. Please elaborate on this. Other than that, it seems like a solid piece of work.
lib/fake-xhr/index.test.js
Outdated
this.xhr = new FakeXMLHttpRequest(); | ||
}); | ||
|
||
it("property is set if we support timeout", function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
weird wording, but I see another similar test is above it, so I understand you just stuck to the "standard". try changing it to "should set the property if ...", and if you could fix the other one while you are at it, that would be great 🙏
assert.isFalse(this.xhr.triggerTimeout.called); | ||
}); | ||
|
||
it("only gets called with fake timers", function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really understand this restriction. Why would we want to restrict this functionality to fake timers - shouldn't this be up to the programmer if she want to control time?
Thanks for taking the time to look at the code. I've rebased to fix the wording on the test. As for my decision to restrict normal timers, it was mostly a personal decision based on my expectations of how a mocked If we were to always enable timeouts, there might be some backward compatibility issues for some users. Due to performance reasons, they may see different results from their tests depending on their environment (CI or local). Although, this would assume the user is already using non-blocking behavior (like promises) in their tests. With that said, I'm not sure what the correct behavior should be. I think the argument that it should be up to the user to specify whether timers should or shouldn't be faked at the beginning of their tests is definitely reasonable. I also find the condition to check if the timers are faked is also somewhat fragile. So I'd say that's another good reason to remove the condition. Let me know if anything I've said has changed your opinion. If not, I'd be happy to remove the condition and change the tests, accordingly. |
Thanks! |
This is my attempt to resolve: #3.
I've added a
supportsTimeout
flag tosinonXhr
. When callingsend
, there is a check to see if the fake timers exist and if so, to listen for ticks throughsetInterval
.When abort or timeout occur, they follow a lot of the same steps. Only difference really being that abort sets the readyState to UNSENT. For this reason, I've refactored a number of things around abort to keep their behavior the same. This includes refactoring some of the tests.
For more details, see
requestErrorSteps
andassertRequestErrorSteps
.Some things that you might find questionable are:
The way I'm checking to see if the timers are fake. I'm really just checking to see if the
clock
property exists on the methods I need. This is whatfakeServerWithClocks
does. I'd be happy to know if there's a more reliable method to test this. It doesn't seem like there is any global clock reference that can be tested against.I think a lot of people might find this syntax to be very gross (https://github.com/Munksey/nise/blob/6c3afbb38a18cf5009a7baed47d9eee8d5f117fd/lib/fake-xhr/index.js#L405). Have no problems changing that to a less condensed if statement.
Let me know if you want me to annotate the PR with github comments. I've added some comments in the code that hopefully explains the rest of my reasoning.