Skip to content
This repository was archived by the owner on Jun 16, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"@types/jest": "^26.0.24",
"@types/lodash": "^4.14.194",
"@types/node": "^18.19.17",
"@types/sinon": "^9.0.10",
"eslint": "^9.39.1",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.3",
Expand All @@ -96,8 +95,7 @@
"nock": "^14.0.0-beta.7",
"prettier": "^3.3.3",
"prettier-plugin-packagejson": "^2.4.3",
"sinon": "^9.2.4",
"ts-jest": "^29.1.4",
"ts-jest": "^29.2.5",
"typescript": "~5.3.3",
"typescript-eslint": "^8.39.0"
},
Expand Down
22 changes: 9 additions & 13 deletions src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import type {
import type { TransactionParams } from '@metamask/transaction-controller';
import type { Hex } from '@metamask/utils';
import nock from 'nock';
import * as sinon from 'sinon';

import packageJson from '../package.json';
import {
Expand Down Expand Up @@ -2358,15 +2357,12 @@ describe('SmartTransactionsController', () => {
});

describe('startPolling', () => {
let clock: sinon.SinonFakeTimers;

beforeEach(() => {
// eslint-disable-next-line import-x/namespace
clock = sinon.useFakeTimers();
jest.useFakeTimers();
});

afterEach(() => {
clock.restore();
jest.useRealTimers();
});

it('starts and stops calling smart transactions batch status api endpoint with the correct chainId at the polling interval', async () => {
Expand Down Expand Up @@ -2413,7 +2409,7 @@ describe('SmartTransactionsController', () => {
chainIds: [ChainId.mainnet],
});

await advanceTime({ clock, duration: 0 });
await advanceTime({ duration: 0 });

const fetchHeaders = {
headers: {
Expand All @@ -2430,7 +2426,7 @@ describe('SmartTransactionsController', () => {
fetchHeaders,
);

await advanceTime({ clock, duration: DEFAULT_INTERVAL });
await advanceTime({ duration: DEFAULT_INTERVAL });

expect(handleFetchSpy).toHaveBeenNthCalledWith(
2,
Expand All @@ -2441,7 +2437,7 @@ describe('SmartTransactionsController', () => {
);

controller.startPolling({ chainIds: [ChainId.sepolia] });
await advanceTime({ clock, duration: 0 });
await advanceTime({ duration: 0 });

expect(handleFetchSpy).toHaveBeenNthCalledWith(
3,
Expand All @@ -2451,7 +2447,7 @@ describe('SmartTransactionsController', () => {
fetchHeaders,
);

await advanceTime({ clock, duration: DEFAULT_INTERVAL });
await advanceTime({ duration: DEFAULT_INTERVAL });

expect(handleFetchSpy).toHaveBeenNthCalledWith(
5,
Expand All @@ -2465,9 +2461,9 @@ describe('SmartTransactionsController', () => {
controller.stopPollingByPollingToken(mainnetPollingToken);

// cycle two polling intervals
await advanceTime({ clock, duration: DEFAULT_INTERVAL });
await advanceTime({ duration: DEFAULT_INTERVAL });

await advanceTime({ clock, duration: DEFAULT_INTERVAL });
await advanceTime({ duration: DEFAULT_INTERVAL });

// check that the mainnet polling has stopped while the sepolia polling continues
expect(handleFetchSpy).toHaveBeenNthCalledWith(
Expand Down Expand Up @@ -2533,7 +2529,7 @@ describe('SmartTransactionsController', () => {
chainIds: ['notSupportedChainId' as Hex],
});

await advanceTime({ clock, duration: 0 });
await advanceTime({ duration: 0 });

expect(handleFetchSpy).not.toHaveBeenCalled();
},
Expand Down
7 changes: 2 additions & 5 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,25 @@ export const flushPromises = async () => {
};

/**
* Advances the provided fake timer by a specified duration in incremental steps.
* Advances Jest's fake timer by a specified duration in incremental steps.
* Between each step, any enqueued promises are processed. Fake timers in testing libraries
* allow simulation of time without actually waiting. However, they don't always account for
* promises or other asynchronous operations that may get enqueued during the timer's duration.
* By advancing time in incremental steps and flushing promises between each step,
* this function ensures that both timers and promises are comprehensively processed.
* @param options - The options object.
* @param options.clock - The Sinon fake timer instance used to manipulate time in tests.
* @param options.duration - The total amount of time (in milliseconds) to advance the timer by.
* @param options.stepSize - The incremental step size (in milliseconds) by which the timer is advanced in each iteration. Default is 1/4 of the duration.
*/
export async function advanceTime({
clock,
duration,
stepSize = Math.floor(duration / 4),
}: {
clock: sinon.SinonFakeTimers;
duration: number;
stepSize?: number;
}): Promise<void> {
do {
await clock.tickAsync(stepSize);
await jest.advanceTimersByTimeAsync(stepSize);
await flushPromises();
// eslint-disable-next-line no-param-reassign
duration -= stepSize;
Expand Down
Loading
Loading