Skip to content

Commit f43c335

Browse files
committed
fix(core-backend): control randomness to fix flaky test
1 parent 796d640 commit f43c335

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

packages/core-backend/src/BackendWebSocketService.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,9 @@ describe('BackendWebSocketService', () => {
817817
it('should skip connect when reconnect timer is already scheduled', async () => {
818818
await withService(
819819
async ({ service, getMockWebSocket, completeAsyncOperations }) => {
820+
// Mock Math.random to make Cockatiel's jitter deterministic
821+
jest.spyOn(Math, 'random').mockReturnValue(0);
822+
820823
// Connect successfully first
821824
await service.connect();
822825

@@ -952,6 +955,9 @@ describe('BackendWebSocketService', () => {
952955
it('should not schedule multiple reconnects when scheduleReconnect called multiple times', async () => {
953956
await withService(
954957
async ({ service, getMockWebSocket, completeAsyncOperations }) => {
958+
// Mock Math.random to make Cockatiel's jitter deterministic
959+
jest.spyOn(Math, 'random').mockReturnValue(0);
960+
955961
await service.connect();
956962
expect(service.getConnectionInfo().state).toBe(
957963
WebSocketState.CONNECTED,
@@ -987,6 +993,9 @@ describe('BackendWebSocketService', () => {
987993
it('should force reconnection and schedule connect', async () => {
988994
await withService(
989995
async ({ service, getMockWebSocket, completeAsyncOperations }) => {
996+
// Mock Math.random to make Cockatiel's jitter deterministic
997+
jest.spyOn(Math, 'random').mockReturnValue(0);
998+
990999
await service.connect();
9911000
expect(service.getConnectionInfo().state).toBe(
9921001
WebSocketState.CONNECTED,
@@ -1018,6 +1027,9 @@ describe('BackendWebSocketService', () => {
10181027
await withService(
10191028
{ mockWebSocketOptions: { autoConnect: false } },
10201029
async ({ service, getMockWebSocket, completeAsyncOperations }) => {
1030+
// Mock Math.random to make Cockatiel's jitter deterministic
1031+
jest.spyOn(Math, 'random').mockReturnValue(0);
1032+
10211033
// Trigger a connection failure to schedule a reconnect
10221034
// eslint-disable-next-line @typescript-eslint/no-floating-promises
10231035
service.connect();

0 commit comments

Comments
 (0)