diff --git a/packages/polling-controller/package.json b/packages/polling-controller/package.json index 7b78954fd01..77f502c8349 100644 --- a/packages/polling-controller/package.json +++ b/packages/polling-controller/package.json @@ -62,7 +62,6 @@ "@types/jest": "^29.5.14", "deepmerge": "^4.2.2", "jest": "^29.7.0", - "sinon": "^9.2.4", "ts-jest": "^29.2.5", "typedoc": "^0.25.13", "typedoc-plugin-missing-exports": "^2.0.0", diff --git a/packages/polling-controller/src/BlockTrackerPollingController.test.ts b/packages/polling-controller/src/BlockTrackerPollingController.test.ts index 927fb92d27e..803ef553075 100644 --- a/packages/polling-controller/src/BlockTrackerPollingController.test.ts +++ b/packages/polling-controller/src/BlockTrackerPollingController.test.ts @@ -2,7 +2,6 @@ import { MOCK_ANY_NAMESPACE, Messenger } from '@metamask/messenger'; import type { MockAnyNamespace } from '@metamask/messenger'; import type { NetworkClient } from '@metamask/network-controller'; import EventEmitter from 'events'; -import { useFakeTimers } from 'sinon'; import type { BlockTrackerPollingInput } from './BlockTrackerPollingController'; import { BlockTrackerPollingController } from './BlockTrackerPollingController'; @@ -43,13 +42,13 @@ class TestBlockTracker extends EventEmitter { } describe('BlockTrackerPollingController', () => { - let clock: sinon.SinonFakeTimers; let mockMessenger: Messenger; let controller: ChildBlockTrackerPollingController; let mainnetBlockTracker: TestBlockTracker; let goerliBlockTracker: TestBlockTracker; let sepoliaBlockTracker: TestBlockTracker; beforeEach(() => { + jest.useFakeTimers({ doNotFake: ['nextTick', 'queueMicrotask'] }); mockMessenger = new Messenger({ namespace: MOCK_ANY_NAMESPACE }); controller = new ChildBlockTrackerPollingController({ messenger: mockMessenger, @@ -82,10 +81,9 @@ describe('BlockTrackerPollingController', () => { throw new Error(`Unknown networkClientId: ${networkClientId}`); } }); - clock = useFakeTimers(); }); afterEach(() => { - clock.restore(); + jest.useRealTimers(); }); describe('startPolling', () => { diff --git a/packages/polling-controller/src/StaticIntervalPollingController.test.ts b/packages/polling-controller/src/StaticIntervalPollingController.test.ts index 4730fb87117..e9e55b2102d 100644 --- a/packages/polling-controller/src/StaticIntervalPollingController.test.ts +++ b/packages/polling-controller/src/StaticIntervalPollingController.test.ts @@ -1,10 +1,9 @@ import { MOCK_ANY_NAMESPACE, Messenger } from '@metamask/messenger'; import type { MockAnyNamespace } from '@metamask/messenger'; import { createDeferredPromise } from '@metamask/utils'; -import { useFakeTimers } from 'sinon'; import { StaticIntervalPollingController } from './StaticIntervalPollingController'; -import { advanceTime } from '../../../tests/helpers'; +import { jestAdvanceTime } from '../../../tests/helpers'; const TICK_TIME = 5; @@ -39,10 +38,10 @@ class ChildBlockTrackerPollingController extends StaticIntervalPollingController } describe('StaticIntervalPollingController', () => { - let clock: sinon.SinonFakeTimers; let mockMessenger: Messenger; let controller: ChildBlockTrackerPollingController; beforeEach(() => { + jest.useFakeTimers({ doNotFake: ['nextTick', 'queueMicrotask'] }); mockMessenger = new Messenger({ namespace: MOCK_ANY_NAMESPACE }); controller = new ChildBlockTrackerPollingController({ messenger: mockMessenger, @@ -51,36 +50,35 @@ describe('StaticIntervalPollingController', () => { state: { foo: 'bar' }, }); controller.setIntervalLength(TICK_TIME); - clock = useFakeTimers(); }); afterEach(() => { - clock.restore(); + jest.useRealTimers(); }); describe('startPolling', () => { it('should start polling if not already polling', async () => { controller.startPolling({ networkClientId: 'mainnet' }); - await advanceTime({ clock, duration: 0 }); + await jestAdvanceTime({ duration: 0 }); expect(controller._executePoll).toHaveBeenCalledTimes(1); controller.executePollPromises[0].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); expect(controller._executePoll).toHaveBeenCalledTimes(2); controller.stopAllPolling(); }); it('should call _executePoll immediately once and continue calling _executePoll on interval when called again with the same networkClientId', async () => { controller.startPolling({ networkClientId: 'mainnet' }); - await advanceTime({ clock, duration: 0 }); + await jestAdvanceTime({ duration: 0 }); controller.startPolling({ networkClientId: 'mainnet' }); - await advanceTime({ clock, duration: 0 }); + await jestAdvanceTime({ duration: 0 }); expect(controller._executePoll).toHaveBeenCalledTimes(1); controller.executePollPromises[0].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); controller.executePollPromises[1].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); controller.executePollPromises[2].resolve(); expect(controller._executePoll).toHaveBeenCalledTimes(3); @@ -92,12 +90,12 @@ describe('StaticIntervalPollingController', () => { controller.startPolling({ networkClientId: 'mainnet', }); - await advanceTime({ clock, duration: 0 }); + await jestAdvanceTime({ duration: 0 }); controller.startPolling({ networkClientId: 'rinkeby', }); - await advanceTime({ clock, duration: 0 }); + await jestAdvanceTime({ duration: 0 }); expect(controller._executePoll.mock.calls).toMatchObject([ [{ networkClientId: 'mainnet' }], @@ -106,7 +104,7 @@ describe('StaticIntervalPollingController', () => { controller.executePollPromises[0].resolve(); controller.executePollPromises[1].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); expect(controller._executePoll.mock.calls).toMatchObject([ [{ networkClientId: 'mainnet' }], @@ -117,7 +115,7 @@ describe('StaticIntervalPollingController', () => { controller.executePollPromises[2].resolve(); controller.executePollPromises[3].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); expect(controller._executePoll.mock.calls).toMatchObject([ [{ networkClientId: 'mainnet' }], @@ -135,18 +133,18 @@ describe('StaticIntervalPollingController', () => { controller.startPolling({ networkClientId: 'mainnet', }); - await advanceTime({ clock, duration: 0 }); + await jestAdvanceTime({ duration: 0 }); expect(controller._executePoll.mock.calls).toMatchObject([ [{ networkClientId: 'mainnet' }], ]); controller.executePollPromises[0].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); controller.startPolling({ networkClientId: 'sepolia', }); - await advanceTime({ clock, duration: 0 }); + await jestAdvanceTime({ duration: 0 }); expect(controller._executePoll.mock.calls).toMatchObject([ [{ networkClientId: 'mainnet' }], @@ -154,7 +152,7 @@ describe('StaticIntervalPollingController', () => { ]); controller.executePollPromises[1].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); expect(controller._executePoll.mock.calls).toMatchObject([ [{ networkClientId: 'mainnet' }], @@ -163,7 +161,7 @@ describe('StaticIntervalPollingController', () => { ]); controller.executePollPromises[2].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); expect(controller._executePoll.mock.calls).toMatchObject([ [{ networkClientId: 'mainnet' }], @@ -173,7 +171,7 @@ describe('StaticIntervalPollingController', () => { ]); controller.executePollPromises[3].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); expect(controller._executePoll.mock.calls).toMatchObject([ [{ networkClientId: 'mainnet' }], @@ -184,7 +182,7 @@ describe('StaticIntervalPollingController', () => { ]); controller.executePollPromises[4].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); expect(controller._executePoll.mock.calls).toMatchObject([ [{ networkClientId: 'mainnet' }], @@ -203,12 +201,12 @@ describe('StaticIntervalPollingController', () => { const pollingToken = controller.startPolling({ networkClientId: 'mainnet', }); - await advanceTime({ clock, duration: 0 }); + await jestAdvanceTime({ duration: 0 }); expect(controller._executePoll).toHaveBeenCalledTimes(1); controller.executePollPromises[0].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); controller.stopPollingByPollingToken(pollingToken); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); expect(controller._executePoll).toHaveBeenCalledTimes(2); controller.stopAllPolling(); }); @@ -217,15 +215,15 @@ describe('StaticIntervalPollingController', () => { const pollingToken1 = controller.startPolling({ networkClientId: 'mainnet', }); - await advanceTime({ clock, duration: 0 }); + await jestAdvanceTime({ duration: 0 }); controller.startPolling({ networkClientId: 'mainnet' }); expect(controller._executePoll).toHaveBeenCalledTimes(1); controller.executePollPromises[0].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); controller.stopPollingByPollingToken(pollingToken1); controller.executePollPromises[1].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); expect(controller._executePoll).toHaveBeenCalledTimes(3); controller.stopAllPolling(); }); @@ -243,18 +241,18 @@ describe('StaticIntervalPollingController', () => { networkClientId: 'mainnet', address: '0x1', }); - await advanceTime({ clock, duration: 0 }); + await jestAdvanceTime({ duration: 0 }); controller.startPolling({ networkClientId: 'mainnet', address: '0x2', }); - await advanceTime({ clock, duration: 0 }); + await jestAdvanceTime({ duration: 0 }); controller.startPolling({ networkClientId: 'sepolia', address: '0x2', }); - await advanceTime({ clock, duration: 0 }); + await jestAdvanceTime({ duration: 0 }); expect(controller._executePoll.mock.calls).toMatchObject([ [{ networkClientId: 'mainnet', address: '0x1' }], @@ -265,7 +263,7 @@ describe('StaticIntervalPollingController', () => { controller.executePollPromises[0].resolve(); controller.executePollPromises[1].resolve(); controller.executePollPromises[2].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); expect(controller._executePoll.mock.calls).toMatchObject([ [{ networkClientId: 'mainnet', address: '0x1' }], @@ -279,7 +277,7 @@ describe('StaticIntervalPollingController', () => { controller.executePollPromises[3].resolve(); controller.executePollPromises[4].resolve(); controller.executePollPromises[5].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); expect(controller._executePoll.mock.calls).toMatchObject([ [{ networkClientId: 'mainnet', address: '0x1' }], @@ -297,13 +295,13 @@ describe('StaticIntervalPollingController', () => { const pollingToken = controller.startPolling({ networkClientId: 'mainnet', }); - await advanceTime({ clock, duration: 0 }); + await jestAdvanceTime({ duration: 0 }); expect(controller._executePoll).toHaveBeenCalledTimes(1); controller.stopPollingByPollingToken(pollingToken); controller.executePollPromises[0].resolve(); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); expect(controller._executePoll).toHaveBeenCalledTimes(1); - await advanceTime({ clock, duration: TICK_TIME }); + await jestAdvanceTime({ duration: TICK_TIME }); expect(controller._executePoll).toHaveBeenCalledTimes(1); controller.stopAllPolling(); }); diff --git a/yarn.lock b/yarn.lock index 5b7987d11fc..f7f2c01da51 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4408,7 +4408,6 @@ __metadata: deepmerge: "npm:^4.2.2" fast-json-stable-stringify: "npm:^2.1.0" jest: "npm:^29.7.0" - sinon: "npm:^9.2.4" ts-jest: "npm:^29.2.5" typedoc: "npm:^0.25.13" typedoc-plugin-missing-exports: "npm:^2.0.0"