Skip to content

fix(test): Fix Math.random flaky test #108

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

Merged
merged 1 commit into from
May 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions test/simple_socketio.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,13 @@ describe("Tests using SimpleSocketIOClient", () => {
// Reconnect should not be called yet
expect(client.attemptReconnect).toHaveBeenCalledTimes(0);
});
test("reconnect method exponentially increase delay for every attempt", () => {
test("reconnect method exponentially increase delay for every attempt, stopping at the max value", () => {
const originalRandom = Math.random;
Math.random = vi.fn().mockReturnValue(1);
vi.useFakeTimers();
vi.spyOn(client, "attemptReconnect");

const reconnectAttempts = 5;
const reconnectAttempts = 10;
for (let i = 0; i <= reconnectAttempts; i++) {
const expectedMinDelay = Math.min(1000 * Math.pow(2, i), 10000);
const expectedMaxDelay = expectedMinDelay * 1.5;
Expand All @@ -468,6 +470,7 @@ describe("Tests using SimpleSocketIOClient", () => {
}
vi.runOnlyPendingTimers();
vi.useRealTimers();
Math.random = originalRandom;
});
test("reconnect method schedules reconnect only once and calls reconnect after specified delay", () => {
vi.useFakeTimers();
Expand Down