From adf9a5761d21ad8f4b16f0bce669986b0d8c32e8 Mon Sep 17 00:00:00 2001 From: Salah-Eddine Saakoun Date: Wed, 18 Feb 2026 09:23:36 -0800 Subject: [PATCH 1/3] chore(ramps-controller): replace `Sinon` with `Jest` fake timers --- packages/ramps-controller/package.json | 2 - .../ramps-controller/src/RampsService.test.ts | 162 +++++++++--------- .../src/TransakService.test.ts | 133 +++++++------- yarn.lock | 18 -- 4 files changed, 142 insertions(+), 173 deletions(-) diff --git a/packages/ramps-controller/package.json b/packages/ramps-controller/package.json index 417b3f8898c..da9f373834e 100644 --- a/packages/ramps-controller/package.json +++ b/packages/ramps-controller/package.json @@ -57,11 +57,9 @@ "@metamask/auto-changelog": "^3.4.4", "@ts-bridge/cli": "^0.6.4", "@types/jest": "^29.5.14", - "@types/sinon": "^9.0.10", "deepmerge": "^4.2.2", "jest": "^29.7.0", "nock": "^13.3.1", - "sinon": "^9.2.4", "ts-jest": "^29.2.5", "typedoc": "^0.25.13", "typedoc-plugin-missing-exports": "^2.0.0", diff --git a/packages/ramps-controller/src/RampsService.test.ts b/packages/ramps-controller/src/RampsService.test.ts index a99c37d9353..663af0dc833 100644 --- a/packages/ramps-controller/src/RampsService.test.ts +++ b/packages/ramps-controller/src/RampsService.test.ts @@ -5,8 +5,6 @@ import type { MessengerEvents, } from '@metamask/messenger'; import nock from 'nock'; -import { useFakeTimers } from 'sinon'; -import type { SinonFakeTimers } from 'sinon'; import type { RampsServiceMessenger } from './RampsService'; import { RampsService, RampsEnvironment } from './RampsService'; @@ -16,14 +14,12 @@ import packageJson from '../package.json'; const CONTROLLER_VERSION = packageJson.version; describe('RampsService', () => { - let clock: SinonFakeTimers; - beforeEach(() => { - clock = useFakeTimers(); + jest.useFakeTimers({ doNotFake: ['nextTick', 'queueMicrotask'] }); }); afterEach(() => { - clock.restore(); + jest.useRealTimers(); }); describe('RampsService:getGeolocation', () => { @@ -41,7 +37,7 @@ describe('RampsService', () => { const geolocationPromise = rootMessenger.call( 'RampsService:getGeolocation', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const geolocationResponse = await geolocationPromise; @@ -64,7 +60,7 @@ describe('RampsService', () => { const geolocationPromise = rootMessenger.call( 'RampsService:getGeolocation', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const geolocationResponse = await geolocationPromise; @@ -87,7 +83,7 @@ describe('RampsService', () => { const geolocationPromise = rootMessenger.call( 'RampsService:getGeolocation', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const geolocationResponse = await geolocationPromise; @@ -110,7 +106,7 @@ describe('RampsService', () => { const geolocationPromise = rootMessenger.call( 'RampsService:getGeolocation', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const geolocationResponse = await geolocationPromise; @@ -133,7 +129,7 @@ describe('RampsService', () => { const geolocationPromise = rootMessenger.call( 'RampsService:getGeolocation', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const geolocationResponse = await geolocationPromise; @@ -154,7 +150,7 @@ describe('RampsService', () => { const geolocationPromise = rootMessenger.call( 'RampsService:getGeolocation', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(geolocationPromise).rejects.toThrow( 'Malformed response received from geolocation API', @@ -173,13 +169,13 @@ describe('RampsService', () => { .reply(500, 'Internal Server Error'); const { service, rootMessenger } = getService(); service.onRetry(() => { - clock.nextAsync().catch(() => undefined); + jest.advanceTimersToNextTimerAsync().catch(() => undefined); }); const geolocationPromise = rootMessenger.call( 'RampsService:getGeolocation', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(geolocationPromise).rejects.toThrow( `Fetching 'https://on-ramp.uat-api.cx.metamask.io/geolocation?sdk=2.1.6&controller=${CONTROLLER_VERSION}&context=mobile-ios' failed with status '500'`, @@ -208,9 +204,9 @@ describe('RampsService', () => { const geolocationPromise = rootMessenger.call( 'RampsService:getGeolocation', ); - await clock.tickAsync(6000); + await jest.advanceTimersByTimeAsync(6000); await flushPromises(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const geolocationResponse = await geolocationPromise; @@ -230,13 +226,13 @@ describe('RampsService', () => { .reply(500); const { service, rootMessenger } = getService(); service.onRetry(() => { - clock.nextAsync().catch(() => undefined); + jest.advanceTimersToNextTimerAsync().catch(() => undefined); }); const geolocationPromise = rootMessenger.call( 'RampsService:getGeolocation', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(geolocationPromise).rejects.toThrow( `Fetching 'https://on-ramp.uat-api.cx.metamask.io/geolocation?sdk=2.1.6&controller=${CONTROLLER_VERSION}&context=mobile-ios' failed with status '500'`, @@ -260,7 +256,7 @@ describe('RampsService', () => { }, }); service.onRetry(() => { - clock.nextAsync().catch(() => undefined); + jest.advanceTimersToNextTimerAsync().catch(() => undefined); }); await expect( @@ -282,7 +278,7 @@ describe('RampsService', () => { const { service } = getService(); const geolocationPromise = service.getGeolocation(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const geolocationResponse = await geolocationPromise; @@ -331,7 +327,7 @@ describe('RampsService', () => { const { rootMessenger } = getService(); const countriesPromise = rootMessenger.call('RampsService:getCountries'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const countriesResponse = await countriesPromise; @@ -386,7 +382,7 @@ describe('RampsService', () => { }); const countriesPromise = rootMessenger.call('RampsService:getCountries'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const countriesResponse = await countriesPromise; @@ -441,7 +437,7 @@ describe('RampsService', () => { }); const countriesPromise = rootMessenger.call('RampsService:getCountries'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const countriesResponse = await countriesPromise; @@ -519,7 +515,7 @@ describe('RampsService', () => { const { service } = getService(); const countriesPromise = service.getCountries(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const countriesResponse = await countriesPromise; @@ -567,7 +563,7 @@ describe('RampsService', () => { const { service } = getService(); const countriesPromise = service.getCountries(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const countriesResponse = await countriesPromise; @@ -587,11 +583,11 @@ describe('RampsService', () => { .reply(500); const { service, rootMessenger } = getService(); service.onRetry(() => { - clock.nextAsync().catch(() => undefined); + jest.advanceTimersToNextTimerAsync().catch(() => undefined); }); const countriesPromise = rootMessenger.call('RampsService:getCountries'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(countriesPromise).rejects.toThrow( `Fetching 'https://on-ramp-cache.uat-api.cx.metamask.io/v2/regions/countries?sdk=2.1.6&controller=${CONTROLLER_VERSION}&context=mobile-ios' failed with status '500'`, @@ -610,7 +606,7 @@ describe('RampsService', () => { const { rootMessenger } = getService(); const countriesPromise = rootMessenger.call('RampsService:getCountries'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(countriesPromise).rejects.toThrow( 'Malformed response received from countries API', @@ -629,7 +625,7 @@ describe('RampsService', () => { const { rootMessenger } = getService(); const countriesPromise = rootMessenger.call('RampsService:getCountries'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(countriesPromise).rejects.toThrow( 'Malformed response received from countries API', @@ -660,7 +656,7 @@ describe('RampsService', () => { const { service } = getService(); const countriesPromise = service.getCountries(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const countriesResponse = await countriesPromise; @@ -726,7 +722,7 @@ describe('RampsService', () => { const { service } = getService(); const countriesPromise = service.getCountries(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const countriesResponse = await countriesPromise; @@ -775,7 +771,7 @@ describe('RampsService', () => { const { service } = getService(); const countriesPromise = service.getCountries(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const countriesResponse = await countriesPromise; @@ -820,7 +816,7 @@ describe('RampsService', () => { const { service } = getService(); const countriesPromise = service.getCountries(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const countriesResponse = await countriesPromise; @@ -879,7 +875,7 @@ describe('RampsService', () => { const { service } = getService(); const tokensPromise = service.getTokens('us', 'buy'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const tokensResponse = await tokensPromise; @@ -936,7 +932,7 @@ describe('RampsService', () => { const { service } = getService(); const tokensPromise = service.getTokens('us'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const tokensResponse = await tokensPromise; @@ -969,7 +965,7 @@ describe('RampsService', () => { const { service } = getService(); const tokensPromise = service.getTokens('US', 'buy'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const tokensResponse = await tokensPromise; @@ -1002,7 +998,7 @@ describe('RampsService', () => { const { service } = getService(); const tokensPromise = service.getTokens('us', 'sell'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const tokensResponse = await tokensPromise; @@ -1031,7 +1027,7 @@ describe('RampsService', () => { const { service } = getService(); const tokensPromise = service.getTokens('us', 'buy'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(tokensPromise).rejects.toThrow( @@ -1060,7 +1056,7 @@ describe('RampsService', () => { const { service } = getService(); const tokensPromise = service.getTokens('us', 'buy'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(tokensPromise).rejects.toThrow( @@ -1089,7 +1085,7 @@ describe('RampsService', () => { const { service } = getService(); const tokensPromise = service.getTokens('us', 'buy'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(tokensPromise).rejects.toThrow( @@ -1118,7 +1114,7 @@ describe('RampsService', () => { const { service } = getService(); const tokensPromise = service.getTokens('us', 'buy'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(tokensPromise).rejects.toThrow( @@ -1154,7 +1150,7 @@ describe('RampsService', () => { const tokensPromise = service.getTokens('us', 'buy', { provider: 'provider-id', }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const tokensResponse = await tokensPromise; @@ -1190,7 +1186,7 @@ describe('RampsService', () => { const tokensPromise = service.getTokens('us', 'buy', { provider: ['provider-id-1', 'provider-id-2'], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const tokensResponse = await tokensPromise; @@ -1211,11 +1207,11 @@ describe('RampsService', () => { .reply(500, 'Internal Server Error'); const { service } = getService(); service.onRetry(() => { - clock.nextAsync().catch(() => undefined); + jest.advanceTimersToNextTimerAsync().catch(() => undefined); }); const tokensPromise = service.getTokens('us', 'buy'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(tokensPromise).rejects.toThrow( @@ -1255,7 +1251,7 @@ describe('RampsService', () => { const { service } = getService(); const providersPromise = service.getProviders('us'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const providersResponse = await providersPromise; @@ -1280,7 +1276,7 @@ describe('RampsService', () => { const { service } = getService(); const providersPromise = service.getProviders('US'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const providersResponse = await providersPromise; @@ -1311,7 +1307,7 @@ describe('RampsService', () => { fiat: 'USD', payments: 'card', }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const providersResponse = await providersPromise; @@ -1338,7 +1334,7 @@ describe('RampsService', () => { provider: ['paypal', 'ramp'], crypto: ['ETH', 'BTC'], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const providersResponse = await providersPromise; @@ -1365,7 +1361,7 @@ describe('RampsService', () => { fiat: 'USD', payments: 'card', }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const providersResponse = await providersPromise; @@ -1392,7 +1388,7 @@ describe('RampsService', () => { fiat: ['USD', 'EUR'], payments: ['card', 'bank'], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const providersResponse = await providersPromise; @@ -1411,7 +1407,7 @@ describe('RampsService', () => { const { service } = getService(); const providersPromise = service.getProviders('us'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(providersPromise).rejects.toThrow( @@ -1431,7 +1427,7 @@ describe('RampsService', () => { const { service } = getService(); const providersPromise = service.getProviders('us'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(providersPromise).rejects.toThrow( @@ -1451,7 +1447,7 @@ describe('RampsService', () => { const { service } = getService(); const providersPromise = service.getProviders('us'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(providersPromise).rejects.toThrow( @@ -1471,11 +1467,11 @@ describe('RampsService', () => { .reply(500, 'Internal Server Error'); const { service } = getService(); service.onRetry(() => { - clock.nextAsync().catch(() => undefined); + jest.advanceTimersToNextTimerAsync().catch(() => undefined); }); const providersPromise = service.getProviders('us'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(providersPromise).rejects.toThrow( @@ -1537,7 +1533,7 @@ describe('RampsService', () => { assetId: 'eip155:1/slip44:60', provider: '/providers/stripe', }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const paymentMethodsResponse = await paymentMethodsPromise; @@ -1572,7 +1568,7 @@ describe('RampsService', () => { assetId: 'eip155:1/slip44:60', provider: '/providers/stripe', }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const paymentMethodsResponse = await paymentMethodsPromise; @@ -1600,7 +1596,7 @@ describe('RampsService', () => { assetId: 'eip155:1/slip44:60', provider: '/providers/stripe', }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(paymentMethodsPromise).rejects.toThrow( @@ -1629,7 +1625,7 @@ describe('RampsService', () => { assetId: 'eip155:1/slip44:60', provider: '/providers/stripe', }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(paymentMethodsPromise).rejects.toThrow( @@ -1658,7 +1654,7 @@ describe('RampsService', () => { assetId: 'eip155:1/slip44:60', provider: '/providers/stripe', }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(paymentMethodsPromise).rejects.toThrow( @@ -1682,7 +1678,7 @@ describe('RampsService', () => { .reply(500, 'Internal Server Error'); const { service } = getService(); service.onRetry(() => { - clock.nextAsync().catch(() => undefined); + jest.advanceTimersToNextTimerAsync().catch(() => undefined); }); const paymentMethodsPromise = service.getPaymentMethods({ @@ -1691,7 +1687,7 @@ describe('RampsService', () => { assetId: 'eip155:1/slip44:60', provider: '/providers/stripe', }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(paymentMethodsPromise).rejects.toThrow( @@ -1776,7 +1772,7 @@ describe('RampsService', () => { walletAddress: '0x1234567890abcdef1234567890abcdef12345678', paymentMethods: ['/payments/debit-credit-card'], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const quotesResponse = await quotesPromise; @@ -1812,7 +1808,7 @@ describe('RampsService', () => { walletAddress: '0x1234567890abcdef1234567890abcdef12345678', paymentMethods: ['/payments/debit-credit-card'], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const quotesResponse = await quotesPromise; @@ -1848,7 +1844,7 @@ describe('RampsService', () => { '/payments/bank-transfer', ], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const quotesResponse = await quotesPromise; @@ -1883,7 +1879,7 @@ describe('RampsService', () => { paymentMethods: ['/payments/debit-credit-card'], providers: ['/providers/moonpay'], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const quotesResponse = await quotesPromise; @@ -1918,7 +1914,7 @@ describe('RampsService', () => { paymentMethods: ['/payments/debit-credit-card'], providers: ['/providers/moonpay', '/providers/transak'], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const quotesResponse = await quotesPromise; @@ -1953,7 +1949,7 @@ describe('RampsService', () => { paymentMethods: ['/payments/debit-credit-card'], redirectUrl: 'https://example.com/callback', }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const quotesResponse = await quotesPromise; @@ -1987,7 +1983,7 @@ describe('RampsService', () => { paymentMethods: ['/payments/bank-transfer'], action: 'sell', }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const quotesResponse = await quotesPromise; @@ -2020,7 +2016,7 @@ describe('RampsService', () => { walletAddress: '0x1234567890abcdef1234567890abcdef12345678', paymentMethods: ['/payments/debit-credit-card'], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(quotesPromise).rejects.toThrow( @@ -2054,7 +2050,7 @@ describe('RampsService', () => { walletAddress: '0x1234567890abcdef1234567890abcdef12345678', paymentMethods: ['/payments/debit-credit-card'], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(quotesPromise).rejects.toThrow( @@ -2088,7 +2084,7 @@ describe('RampsService', () => { walletAddress: '0x1234567890abcdef1234567890abcdef12345678', paymentMethods: ['/payments/debit-credit-card'], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(quotesPromise).rejects.toThrow( @@ -2127,7 +2123,7 @@ describe('RampsService', () => { walletAddress: '0x1234567890abcdef1234567890abcdef12345678', paymentMethods: ['/payments/debit-credit-card'], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(quotesPromise).rejects.toThrow( @@ -2166,7 +2162,7 @@ describe('RampsService', () => { walletAddress: '0x1234567890abcdef1234567890abcdef12345678', paymentMethods: ['/payments/debit-credit-card'], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(quotesPromise).rejects.toThrow( @@ -2205,7 +2201,7 @@ describe('RampsService', () => { walletAddress: '0x1234567890abcdef1234567890abcdef12345678', paymentMethods: ['/payments/debit-credit-card'], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(quotesPromise).rejects.toThrow( @@ -2232,7 +2228,7 @@ describe('RampsService', () => { .reply(500, 'Internal Server Error'); const { service } = getService(); service.onRetry(() => { - clock.nextAsync().catch(() => undefined); + jest.advanceTimersToNextTimerAsync().catch(() => undefined); }); const quotesPromise = service.getQuotes({ @@ -2243,7 +2239,7 @@ describe('RampsService', () => { walletAddress: '0x1234567890abcdef1234567890abcdef12345678', paymentMethods: ['/payments/debit-credit-card'], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(quotesPromise).rejects.toThrow("failed with status '500'"); @@ -2277,7 +2273,7 @@ describe('RampsService', () => { walletAddress: '0x1234567890abcdef1234567890abcdef12345678', paymentMethods: ['/payments/debit-credit-card'], }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const quotesResponse = await quotesPromise; @@ -2305,7 +2301,7 @@ describe('RampsService', () => { 'RampsService:getBuyWidgetUrl', 'https://on-ramp.uat-api.cx.metamask.io/providers/transak-staging/buy-widget', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const buyWidget = await buyWidgetPromise; @@ -2328,13 +2324,13 @@ describe('RampsService', () => { .reply(500, 'Internal Server Error'); const { service } = getService(); service.onRetry(() => { - clock.nextAsync().catch(() => undefined); + jest.advanceTimersToNextTimerAsync().catch(() => undefined); }); const buyWidgetPromise = service.getBuyWidgetUrl( 'https://on-ramp.uat-api.cx.metamask.io/providers/transak-staging/buy-widget', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(buyWidgetPromise).rejects.toThrow( @@ -2360,7 +2356,7 @@ describe('RampsService', () => { 'RampsService:getBuyWidgetUrl', 'https://on-ramp.uat-api.cx.metamask.io/providers/transak-staging/buy-widget', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(buyWidgetPromise).rejects.toThrow( diff --git a/packages/ramps-controller/src/TransakService.test.ts b/packages/ramps-controller/src/TransakService.test.ts index 60d6e41d2f6..35c86f84ee4 100644 --- a/packages/ramps-controller/src/TransakService.test.ts +++ b/packages/ramps-controller/src/TransakService.test.ts @@ -5,8 +5,6 @@ import type { MessengerEvents, } from '@metamask/messenger'; import nock, { cleanAll, isDone } from 'nock'; -import { useFakeTimers } from 'sinon'; -import type { SinonFakeTimers } from 'sinon'; import type { TransakServiceMessenger, @@ -282,14 +280,12 @@ function nockTranslation( // === Tests === describe('TransakService', () => { - let clock: SinonFakeTimers; - beforeEach(() => { - clock = useFakeTimers(); + jest.useFakeTimers({ now: 0, doNotFake: ['nextTick', 'queueMicrotask'] }); }); afterEach(() => { - clock.restore(); + jest.useRealTimers(); cleanAll(); }); @@ -318,7 +314,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.getActiveOrders(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await promise; expect(isDone()).toBe(true); @@ -336,7 +332,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.getActiveOrders(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await promise; expect(isDone()).toBe(true); @@ -492,7 +488,7 @@ describe('TransakService', () => { service.setAccessToken(validToken); const promise = service.getUserDetails(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toStrictEqual(MOCK_USER_DETAILS); @@ -519,7 +515,7 @@ describe('TransakService', () => { const { service } = getService(); const promise = service.sendUserOtp('test@example.com'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -544,7 +540,7 @@ describe('TransakService', () => { 'TransakService:sendUserOtp', 'user@test.com', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -557,7 +553,7 @@ describe('TransakService', () => { const { service } = getService(); const promise = service.sendUserOtp('invalid'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(promise).rejects.toThrow("failed with status '400'"); @@ -590,7 +586,7 @@ describe('TransakService', () => { '123456', 'state-token', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -616,7 +612,7 @@ describe('TransakService', () => { expect(service.getAccessToken()).toBeNull(); const promise = service.verifyUserOtp('a@b.com', '000000', 'st'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await promise; @@ -629,7 +625,7 @@ describe('TransakService', () => { const { service } = getService(); const promise = service.verifyUserOtp('a@b.com', 'wrong', 'st'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(promise).rejects.toThrow("failed with status '401'"); @@ -646,7 +642,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.logout(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -661,7 +657,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.logout(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -681,7 +677,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.logout(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(promise).rejects.toThrow("failed with status '500'"); @@ -700,7 +696,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.getUserDetails(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -718,7 +714,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.getUserDetails(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toBeDefined(); @@ -753,7 +749,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.patchUser(patchData); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -783,7 +779,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.patchUser(patchData); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toStrictEqual({ success: true }); @@ -801,7 +797,7 @@ describe('TransakService', () => { const promise = service.patchUser({ personalDetails: { firstName: 'Fail' }, }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(promise).rejects.toThrow("failed with status '500'"); @@ -843,7 +839,7 @@ describe('TransakService', () => { 'credit_debit_card', '100', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -870,7 +866,7 @@ describe('TransakService', () => { '/payments/debit-credit-card', '100', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toBeDefined(); @@ -893,7 +889,7 @@ describe('TransakService', () => { const { service } = getService(); const promise = service.getBuyQuote('USD', 'ETH', 'eip155:1', '', '100'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toBeDefined(); @@ -921,7 +917,7 @@ describe('TransakService', () => { chainId: 'eip155:1', fiatCurrencyId: 'USD', }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -939,7 +935,7 @@ describe('TransakService', () => { }); const promise = service.getTranslation({ fiatCurrencyId: 'USD' }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toStrictEqual(MOCK_TRANSLATION); @@ -963,7 +959,7 @@ describe('TransakService', () => { paymentMethod: undefined, cryptoCurrencyId: undefined, }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toBeDefined(); @@ -985,7 +981,7 @@ describe('TransakService', () => { fiatCurrencyId: 'USD', paymentMethod: '/payments/debit-credit-card', }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toStrictEqual(MOCK_TRANSLATION); @@ -1007,7 +1003,7 @@ describe('TransakService', () => { fiatCurrencyId: 'USD', paymentMethod: 'credit_debit_card', }); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toStrictEqual(MOCK_TRANSLATION); @@ -1023,7 +1019,7 @@ describe('TransakService', () => { const promise = service.getTranslation({ fiatCurrencyId: 'USD' }); promise.catch(() => undefined); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(promise).rejects.toThrow("failed with status '500'"); @@ -1045,7 +1041,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.getKycRequirement('quote-123'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -1071,7 +1067,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.getAdditionalRequirements('quote-456'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -1099,7 +1095,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.submitSsnDetails('123-45-6789', 'quote-123'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toStrictEqual({ success: true }); @@ -1131,7 +1127,7 @@ describe('TransakService', () => { 'investment', 'payments', ]); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toBeUndefined(); @@ -1162,7 +1158,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.getIdProofStatus('wfr-123'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toStrictEqual(mockStatus); @@ -1196,7 +1192,7 @@ describe('TransakService', () => { '0x1234', 'credit_debit_card', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -1217,7 +1213,7 @@ describe('TransakService', () => { '0x1234', 'credit_debit_card', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(promise).rejects.toThrow("failed with status '400'"); @@ -1246,7 +1242,7 @@ describe('TransakService', () => { '0x1234', '/payments/debit-credit-card', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toBeDefined(); @@ -1278,14 +1274,14 @@ describe('TransakService', () => { '0x1234', '/payments/debit-credit-card', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toBeDefined(); }); it('retries order creation when the first attempt fails with an existing order error', async () => { - clock.restore(); + jest.useRealTimers(); nockTranslation(); @@ -1324,9 +1320,6 @@ describe('TransakService', () => { expect(result.id).toBe(`${STAGING_PROVIDER_PATH}/orders/order-abc-123`); expect(result.orderType).toBe('DEPOSIT'); - - // eslint-disable-next-line require-atomic-updates - clock = useFakeTimers(); }, 10000); it('throws without retrying when a 409 response does not contain the order-exists error code', async () => { @@ -1347,7 +1340,7 @@ describe('TransakService', () => { '0x1234', 'credit_debit_card', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(promise).rejects.toThrow("failed with status '409'"); @@ -1370,7 +1363,7 @@ describe('TransakService', () => { '0x1234', 'credit_debit_card', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(promise).rejects.toThrow( @@ -1406,7 +1399,7 @@ describe('TransakService', () => { const { service } = getService(); const promise = service.getOrder(depositOrderId, '0x1234'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -1426,7 +1419,7 @@ describe('TransakService', () => { const { service } = getService(); const promise = service.getOrder('raw-order-id', '0x1234'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -1455,7 +1448,7 @@ describe('TransakService', () => { '0x1234', paymentDetails, ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -1483,7 +1476,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.getOrder(depositOrderId, '0x1234'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -1507,7 +1500,7 @@ describe('TransakService', () => { const { service } = getService(); const promise = service.getOrder(depositOrderId, '0x1234'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -1525,7 +1518,7 @@ describe('TransakService', () => { const { service } = getService(); const promise = service.getOrder(depositOrderId, '0x1234'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(promise).rejects.toThrow("failed with status '503'"); @@ -1552,7 +1545,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.getOrder(depositOrderId, '0x1234'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -1579,7 +1572,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.getUserLimits('USD', 'credit_debit_card', 'L2'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -1601,7 +1594,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.getUserLimits('USD', '', 'L2'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toStrictEqual(MOCK_USER_LIMITS); @@ -1625,7 +1618,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.requestOtt(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -1772,7 +1765,7 @@ describe('TransakService', () => { depositOrderId, 'credit_debit_card', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -1796,7 +1789,7 @@ describe('TransakService', () => { 'raw-order-id', 'credit_debit_card', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toStrictEqual({ success: true }); @@ -1822,7 +1815,7 @@ describe('TransakService', () => { 'order-1', '/payments/debit-credit-card', ); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toStrictEqual({ success: true }); @@ -1853,7 +1846,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.cancelOrder(depositOrderId); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toBeUndefined(); @@ -1869,7 +1862,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.cancelOrder('raw-cancel-id'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toBeUndefined(); @@ -1892,7 +1885,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.cancelOrder('bad-order'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); await expect(promise).rejects.toThrow("failed with status '404'"); @@ -1925,7 +1918,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.cancelAllActiveOrders(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toStrictEqual([]); @@ -1957,7 +1950,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.cancelAllActiveOrders(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const errors = await promise; @@ -1982,7 +1975,7 @@ describe('TransakService', () => { .mockRejectedValue('string error value'); const promise = service.cancelAllActiveOrders(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const errors = await promise; @@ -2001,7 +1994,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.cancelAllActiveOrders(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toStrictEqual([]); @@ -2019,7 +2012,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.getActiveOrders(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); const result = await promise; @@ -2051,7 +2044,7 @@ describe('TransakService', () => { authenticateService(service); const promise = service.getActiveOrders(); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toBeDefined(); @@ -2074,7 +2067,7 @@ describe('TransakService', () => { const { service } = getService(); const promise = service.sendUserOtp('a@b.com'); - await clock.runAllAsync(); + await jest.runAllTimersAsync(); await flushPromises(); expect(await promise).toBeDefined(); diff --git a/yarn.lock b/yarn.lock index 829ce3f11b7..b1e2cf565df 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4568,11 +4568,9 @@ __metadata: "@metamask/messenger": "npm:^0.3.0" "@ts-bridge/cli": "npm:^0.6.4" "@types/jest": "npm:^29.5.14" - "@types/sinon": "npm:^9.0.10" deepmerge: "npm:^4.2.2" jest: "npm:^29.7.0" nock: "npm:^13.3.1" - sinon: "npm:^9.2.4" ts-jest: "npm:^29.2.5" typedoc: "npm:^0.25.13" typedoc-plugin-missing-exports: "npm:^2.0.0" @@ -6147,22 +6145,6 @@ __metadata: languageName: node linkType: hard -"@types/sinon@npm:^9.0.10": - version: 9.0.11 - resolution: "@types/sinon@npm:9.0.11" - dependencies: - "@types/sinonjs__fake-timers": "npm:*" - checksum: 10/6f74ddc57c755dedc7c2f8d88e11d2edcdc7acf26bebd475e84b1768bfc4db54ffaaa431e22d242eccd270911a031ec66afac52f3dd6eda48f7ccc902db4fac0 - languageName: node - linkType: hard - -"@types/sinonjs__fake-timers@npm:*": - version: 8.1.5 - resolution: "@types/sinonjs__fake-timers@npm:8.1.5" - checksum: 10/3a0b285fcb8e1eca435266faa27ffff206608b69041022a42857274e44d9305822e85af5e7a43a9fae78d2ab7dc0fcb49f3ae3bda1fa81f0203064dbf5afd4f6 - languageName: node - linkType: hard - "@types/stack-utils@npm:^2.0.0": version: 2.0.3 resolution: "@types/stack-utils@npm:2.0.3" From 6cb904a14983b642426d036ed11824b7fa023e17 Mon Sep 17 00:00:00 2001 From: Salah-Eddine Saakoun Date: Wed, 18 Feb 2026 10:13:21 -0800 Subject: [PATCH 2/3] fix: add back @types/sinon to network-controller --- packages/network-controller/package.json | 1 + yarn.lock | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/packages/network-controller/package.json b/packages/network-controller/package.json index a206fdac703..831d16bb319 100644 --- a/packages/network-controller/package.json +++ b/packages/network-controller/package.json @@ -78,6 +78,7 @@ "@types/jest-when": "^2.7.3", "@types/lodash": "^4.14.191", "@types/node-fetch": "^2.6.12", + "@types/sinon": "^9.0.10", "cockatiel": "^3.1.2", "deep-freeze-strict": "^1.1.1", "deepmerge": "^4.2.2", diff --git a/yarn.lock b/yarn.lock index b1e2cf565df..38468ecff46 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4197,6 +4197,7 @@ __metadata: "@types/jest-when": "npm:^2.7.3" "@types/lodash": "npm:^4.14.191" "@types/node-fetch": "npm:^2.6.12" + "@types/sinon": "npm:^9.0.10" async-mutex: "npm:^0.5.0" cockatiel: "npm:^3.1.2" deep-freeze-strict: "npm:^1.1.1" @@ -6145,6 +6146,22 @@ __metadata: languageName: node linkType: hard +"@types/sinon@npm:^9.0.10": + version: 9.0.11 + resolution: "@types/sinon@npm:9.0.11" + dependencies: + "@types/sinonjs__fake-timers": "npm:*" + checksum: 10/6f74ddc57c755dedc7c2f8d88e11d2edcdc7acf26bebd475e84b1768bfc4db54ffaaa431e22d242eccd270911a031ec66afac52f3dd6eda48f7ccc902db4fac0 + languageName: node + linkType: hard + +"@types/sinonjs__fake-timers@npm:*": + version: 15.0.1 + resolution: "@types/sinonjs__fake-timers@npm:15.0.1" + checksum: 10/9fa7a129742262ef4ea9c3253683453c42c2e210cdff1456e233bc9b59f6d60ef8121b32a00209a1c8c7759f88511862dab1ce340081e432515418fc44d8ec35 + languageName: node + linkType: hard + "@types/stack-utils@npm:^2.0.0": version: 2.0.3 resolution: "@types/stack-utils@npm:2.0.3" From c2a329308569a59c48f4144478ca6b7da8ff62dd Mon Sep 17 00:00:00 2001 From: Salah-Eddine Saakoun Date: Wed, 18 Feb 2026 10:23:56 -0800 Subject: [PATCH 3/3] fix: lockfile --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index 38468ecff46..826ad6273d2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6156,9 +6156,9 @@ __metadata: linkType: hard "@types/sinonjs__fake-timers@npm:*": - version: 15.0.1 - resolution: "@types/sinonjs__fake-timers@npm:15.0.1" - checksum: 10/9fa7a129742262ef4ea9c3253683453c42c2e210cdff1456e233bc9b59f6d60ef8121b32a00209a1c8c7759f88511862dab1ce340081e432515418fc44d8ec35 + version: 8.1.5 + resolution: "@types/sinonjs__fake-timers@npm:8.1.5" + checksum: 10/3a0b285fcb8e1eca435266faa27ffff206608b69041022a42857274e44d9305822e85af5e7a43a9fae78d2ab7dc0fcb49f3ae3bda1fa81f0203064dbf5afd4f6 languageName: node linkType: hard