Open MiniSuperDev opened 1 year ago
+1 Using jest.useFakeTimers()
in tests is also causing problems for me. Aside from updating socketio to address this, it would be great to get some guidance in the form of workarounds (that could be applied to projects using the current version of socketio).
Hi! I don't think useFakeTimers()
is meant to be used with I/O operations, is it?
The following method might be useful:
function waitFor(emitter, event) {
return new Promise((resolve) => {
emitter.once(event, resolve);
});
}
Usage:
test('should work', async () => {
serverSocket.emit('hello', 'world');
await waitFor(clientSocket, "hello");
});
Reference: https://jestjs.io/docs/timer-mocks
Closed due to inactivity, please reopen if needed.
I am seeing this issue too - can we reopen? Or at least provide a workaround
@aarowman could you please explain which kind of tests you would like to write? As I said above, I don't think fake timers are meant to be used with async operations such as HTTP requests.
Hi! I don't think
useFakeTimers()
is meant to be used with I/O operations, is it?
Hi @darrachequesne, I'm curious to know why useFakeTimers()
shouldn't be used with I/O operations -- could you please elaborate on that a bit?
The waitFor
suggestion (thank you 🙇🏻) may work for certain kinds of tests, but definitely not all. There's a related, but imo separate, conversation to be had about whether code that requires those kinds of tests should be re-written in a way that eliminates that need; however, I still think it's valuable to consider what it would take to enable testing of code that uses socket.io in combination with jest.useFakeTimers()
.
My understanding is that one would need to mock the XMLHttpRequest and WebSocket objects created by the Socket.IO client, wouldn't it? So that the responses from the server can be manually injected.
I'm not quite following, but would like to understand what you mean..
Are you suggesting that in order to use jest.useFakeTimers()
, then ...
... one would need to mock the XMLHttpRequest and WebSocket objects created by the Socket.IO client [?]
If so, why would that be necessary?
@janeklb actually, I'm not sure to understand the issue here. Could you please provide a sample test case?
@janeklb actually, I'm not sure to understand the issue here. Could you please provide a sample test case?
The issue is as described in the issue title/body: it's not possible to test socket.io code when using jest.useFakeTimers()
; thats said, I'm not really invested in this issue anymore so I'm going to disengage 👋🏻
Hello, when I use
jest.useFakeTimers()
, I don't know how long I have to advance the timers or what method to call so that the internal code that sends and receives socket.io events will be executedI want to know what I should do, because I want to increase the execution time of the tests, and since I have to write tests for some features that use setTimeouts, and some RxJs methods like debounce, throttle, this is essential for me.
This is a minimal example of jest with typescript.
And this is the way how I write test currently
without Timer Mocks
, but I need to await asetTimeout
promise with N milliseconds, to make it work, the problem is this time is variable, for example if I set 4 milliseconds, sometimes the test pass and other times fail, I think is based on my pc resources and the amount of tests that are running. But without fake timers this increment the total execution time, and I can't calculate how long to wait for when I want to wait for other methods to execute, for example from rxjsdebounceTime
orthrottleTime
Thank you
Platform: