From 2b1841ce2c63a6b47d6905e6318a053906f10489 Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Mon, 24 Jun 2024 08:23:13 -0600 Subject: [PATCH] Enable `resetMocks` Jest configuration option (#4417) ## Explanation Jest has a configuration option `resetMocks`. Equivalent to calling `jest.resetAllMocks()` before each test, it helps to ensure that tests are run in isolation by removing fake implementations for each registered mock function, thereby resetting its state. This option is enabled by default for new Jest projects but has been disabled in this repo for a very long time. For some test suites, `jest.resetAllMocks()` (or some variant) has been added to a `beforeEach` or `afterEach` to simulate this option, but overall this is not the case, and some tests were written which assumed that mock functions were not being reset between tests. This commit enables the aforementioned configuration option, fixes tests that fail as a result of this, and removes manual calls to `jest.resetAllMocks()` (or the like). ## References Fixes #745. ## Changelog (N/A) ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate --------- Co-authored-by: jiexi Co-authored-by: Alex Donesky Co-authored-by: Prithpal Sooriya Co-authored-by: Shane Co-authored-by: Derek Brans Co-authored-by: Monte Lai --- jest.config.packages.js | 3 +- jest.config.scripts.js | 4 + .../src/AccountsController.test.ts | 11 - .../src/ApprovalController.test.ts | 8 +- .../src/CurrencyRateController.test.ts | 1 - .../RatesController/RatesController.test.ts | 6 - .../src/TokenListController.test.ts | 1 - .../src/TokenRatesController.test.ts | 4 - .../src/TokensController.test.ts | 1 - .../src/GasFeeController.test.ts | 261 ++++++++++++++---- .../gas-fee-controller/src/gas-util.test.ts | 4 - .../src/LoggingController.test.ts | 17 +- .../src/NameController.test.ts | 14 +- .../name-controller/src/providers/ens.test.ts | 4 - .../src/providers/etherscan.test.ts | 4 - .../src/providers/lens.test.ts | 4 - .../src/providers/token.test.ts | 4 - .../tests/NetworkController.test.ts | 4 - .../src/PermissionController.test.ts | 4 - .../tests/SelectedNetworkController.test.ts | 4 - .../src/SignatureController.test.ts | 1 - .../src/TransactionController.test.ts | 4 - .../src/gas-flows/LineaGasFeeFlow.test.ts | 2 - .../EtherscanRemoteTransactionSource.test.ts | 2 - .../src/helpers/GasFeePoller.test.ts | 1 - .../helpers/IncomingTransactionHelper.test.ts | 4 - .../helpers/MultichainTrackingHelper.test.ts | 2 - .../helpers/PendingTransactionTracker.test.ts | 2 - .../src/utils/etherscan.test.ts | 4 - .../src/utils/gas.test.ts | 2 - .../src/utils/simulation-api.test.ts | 2 - .../src/utils/simulation.test.ts | 1 - .../src/utils/utils.test.ts | 4 - .../src/utils/validation.test.ts | 4 - .../src/UserOperationController.test.ts | 2 - .../PendingUserOperationTracker.test.ts | 1 - .../helpers/SnapSmartContractAccount.test.ts | 2 - .../src/utils/gas-fees.test.ts | 2 - scripts/create-package/commands.test.ts | 4 - scripts/create-package/utils.test.ts | 4 - 40 files changed, 232 insertions(+), 181 deletions(-) diff --git a/jest.config.packages.js b/jest.config.packages.js index 89c610463f..96bde23bfc 100644 --- a/jest.config.packages.js +++ b/jest.config.packages.js @@ -108,8 +108,7 @@ module.exports = { // "resetMocks" resets all mocks, including mocked modules, to jest.fn(), // between each test case. - // TODO: Enable - // resetMocks: true, + resetMocks: true, // Reset the module registry before running each individual test // resetModules: false, diff --git a/jest.config.scripts.js b/jest.config.scripts.js index a86dd27b94..fe5792a9b6 100644 --- a/jest.config.scripts.js +++ b/jest.config.scripts.js @@ -50,6 +50,10 @@ module.exports = { // // A preset that is used as a base for Jest's configuration // preset: 'ts-jest', + // "resetMocks" resets all mocks, including mocked modules, to jest.fn(), + // between each test case. + resetMocks: true, + // "restoreMocks" restores all mocks created using jest.spyOn to their // original implementations, between each test. It does not affect mocked // modules. diff --git a/packages/accounts-controller/src/AccountsController.test.ts b/packages/accounts-controller/src/AccountsController.test.ts index f82aa0eb33..1a60000192 100644 --- a/packages/accounts-controller/src/AccountsController.test.ts +++ b/packages/accounts-controller/src/AccountsController.test.ts @@ -315,10 +315,6 @@ function setupAccountsController({ } describe('AccountsController', () => { - afterEach(() => { - jest.resetAllMocks(); - }); - describe('onSnapStateChange', () => { it('be used enable an account if the Snap is enabled and not blocked', async () => { const messenger = buildMessenger(); @@ -449,9 +445,6 @@ describe('AccountsController', () => { }); describe('onKeyringStateChange', () => { - afterEach(() => { - jest.clearAllMocks(); - }); it('not update state when only keyring is unlocked without any keyrings', async () => { const messenger = buildMessenger(); const { accountsController } = setupAccountsController({ @@ -1304,10 +1297,6 @@ describe('AccountsController', () => { ); }); - afterEach(() => { - jest.clearAllMocks(); - }); - it('update accounts with normal accounts', async () => { mockUUID.mockReturnValueOnce('mock-id').mockReturnValueOnce('mock-id2'); const messenger = buildMessenger(); diff --git a/packages/approval-controller/src/ApprovalController.test.ts b/packages/approval-controller/src/ApprovalController.test.ts index 11cb6a8b26..fe19089528 100644 --- a/packages/approval-controller/src/ApprovalController.test.ts +++ b/packages/approval-controller/src/ApprovalController.test.ts @@ -2,6 +2,7 @@ import { ControllerMessenger } from '@metamask/base-controller'; import { errorCodes, JsonRpcError } from '@metamask/rpc-errors'; +import { nanoid } from 'nanoid'; import type { AddApprovalOptions, @@ -24,9 +25,9 @@ import { NoApprovalFlowsError, } from './errors'; -jest.mock('nanoid', () => ({ - nanoid: jest.fn(() => 'TestId'), -})); +jest.mock('nanoid'); + +const nanoidMock = jest.mocked(nanoid); const PENDING_APPROVALS_STORE_KEY = 'pendingApprovals'; const APPROVAL_FLOWS_STORE_KEY = 'approvalFlows'; @@ -243,6 +244,7 @@ describe('approval controller', () => { let showApprovalRequest: jest.Mock; beforeEach(() => { + nanoidMock.mockReturnValue('TestId'); jest.spyOn(global.console, 'info').mockImplementation(() => undefined); showApprovalRequest = jest.fn(); diff --git a/packages/assets-controllers/src/CurrencyRateController.test.ts b/packages/assets-controllers/src/CurrencyRateController.test.ts index d322e4be3b..0dd2e82aa8 100644 --- a/packages/assets-controllers/src/CurrencyRateController.test.ts +++ b/packages/assets-controllers/src/CurrencyRateController.test.ts @@ -75,7 +75,6 @@ describe('CurrencyRateController', () => { afterEach(() => { clock.restore(); - jest.restoreAllMocks(); }); it('should set default state', () => { diff --git a/packages/assets-controllers/src/RatesController/RatesController.test.ts b/packages/assets-controllers/src/RatesController/RatesController.test.ts index aea62d27be..17e00f3326 100644 --- a/packages/assets-controllers/src/RatesController/RatesController.test.ts +++ b/packages/assets-controllers/src/RatesController/RatesController.test.ts @@ -90,10 +90,6 @@ function setupRatesController({ describe('RatesController', () => { let clock: sinon.SinonFakeTimers; - afterEach(() => { - jest.resetAllMocks(); - }); - describe('construct', () => { it('constructs the RatesController with default values', () => { const ratesController = setupRatesController({ @@ -116,7 +112,6 @@ describe('RatesController', () => { afterEach(() => { clock.restore(); - jest.restoreAllMocks(); }); it('starts the polling process with default values', async () => { @@ -249,7 +244,6 @@ describe('RatesController', () => { afterEach(() => { clock.restore(); - jest.restoreAllMocks(); }); it('stops the polling process', async () => { diff --git a/packages/assets-controllers/src/TokenListController.test.ts b/packages/assets-controllers/src/TokenListController.test.ts index 790f68b045..d6f04de76d 100644 --- a/packages/assets-controllers/src/TokenListController.test.ts +++ b/packages/assets-controllers/src/TokenListController.test.ts @@ -517,7 +517,6 @@ const getRestrictedMessenger = ( describe('TokenListController', () => { afterEach(() => { - jest.restoreAllMocks(); jest.clearAllTimers(); sinon.restore(); }); diff --git a/packages/assets-controllers/src/TokenRatesController.test.ts b/packages/assets-controllers/src/TokenRatesController.test.ts index 3f8404ae36..4ba07c9cb5 100644 --- a/packages/assets-controllers/src/TokenRatesController.test.ts +++ b/packages/assets-controllers/src/TokenRatesController.test.ts @@ -81,10 +81,6 @@ function buildTokenRatesControllerMessenger( } describe('TokenRatesController', () => { - afterEach(() => { - jest.restoreAllMocks(); - }); - describe('constructor', () => { let clock: sinon.SinonFakeTimers; diff --git a/packages/assets-controllers/src/TokensController.test.ts b/packages/assets-controllers/src/TokensController.test.ts index 38692b6404..3d30994470 100644 --- a/packages/assets-controllers/src/TokensController.test.ts +++ b/packages/assets-controllers/src/TokensController.test.ts @@ -77,7 +77,6 @@ describe('TokensController', () => { afterEach(() => { sinon.restore(); - jest.resetAllMocks(); }); it('should set default state', async () => { diff --git a/packages/gas-fee-controller/src/GasFeeController.test.ts b/packages/gas-fee-controller/src/GasFeeController.test.ts index 2c4d59b02a..84d25a03dc 100644 --- a/packages/gas-fee-controller/src/GasFeeController.test.ts +++ b/packages/gas-fee-controller/src/GasFeeController.test.ts @@ -298,7 +298,6 @@ describe('GasFeeController', () => { // eslint-disable-next-line @typescript-eslint/no-floating-promises blockTracker?.destroy(); sinon.restore(); - jest.clearAllMocks(); }); describe('constructor', () => { @@ -735,12 +734,6 @@ describe('GasFeeController', () => { describe('fetchGasFeeEstimates', () => { describe('when on any network supporting legacy gas estimation api', () => { - const defaultConstructorOptions = { - getIsEIP1559Compatible: jest.fn().mockResolvedValue(false), - getCurrentNetworkLegacyGasAPICompatibility: jest - .fn() - .mockReturnValue(true), - }; const mockDetermineGasFeeCalculations = buildMockGasFeeStateLegacy(); beforeEach(() => { @@ -751,7 +744,10 @@ describe('GasFeeController', () => { it('should call determineGasFeeCalculations correctly', async () => { await setupGasFeeController({ - ...defaultConstructorOptions, + getIsEIP1559Compatible: jest.fn().mockResolvedValue(false), + getCurrentNetworkLegacyGasAPICompatibility: jest + .fn() + .mockReturnValue(true), networkControllerState: { networkConfigurations: { 'AAAA-BBBB-CCCC-DDDD': { @@ -792,7 +788,12 @@ describe('GasFeeController', () => { }); it('should update the state with a fetched set of estimates', async () => { - await setupGasFeeController(defaultConstructorOptions); + await setupGasFeeController({ + getIsEIP1559Compatible: jest.fn().mockResolvedValue(false), + getCurrentNetworkLegacyGasAPICompatibility: jest + .fn() + .mockReturnValue(true), + }); await gasFeeController.fetchGasFeeEstimates(); @@ -802,7 +803,12 @@ describe('GasFeeController', () => { }); it('should return the same data that it puts into state', async () => { - await setupGasFeeController(defaultConstructorOptions); + await setupGasFeeController({ + getIsEIP1559Compatible: jest.fn().mockResolvedValue(false), + getCurrentNetworkLegacyGasAPICompatibility: jest + .fn() + .mockReturnValue(true), + }); const estimateData = await gasFeeController.fetchGasFeeEstimates(); @@ -811,7 +817,10 @@ describe('GasFeeController', () => { it('should call determineGasFeeCalculations correctly when getChainId returns a number input', async () => { await setupGasFeeController({ - ...defaultConstructorOptions, + getIsEIP1559Compatible: jest.fn().mockResolvedValue(false), + getCurrentNetworkLegacyGasAPICompatibility: jest + .fn() + .mockReturnValue(true), getChainId: jest.fn().mockReturnValue(1), }); @@ -826,7 +835,10 @@ describe('GasFeeController', () => { it('should call determineGasFeeCalculations correctly when getChainId returns a hexstring input', async () => { await setupGasFeeController({ - ...defaultConstructorOptions, + getIsEIP1559Compatible: jest.fn().mockResolvedValue(false), + getCurrentNetworkLegacyGasAPICompatibility: jest + .fn() + .mockReturnValue(true), getChainId: jest.fn().mockReturnValue('0x1'), }); @@ -841,7 +853,10 @@ describe('GasFeeController', () => { it('should call determineGasFeeCalculations correctly when nonRPCGasFeeApisDisabled is true', async () => { await setupGasFeeController({ - ...defaultConstructorOptions, + getIsEIP1559Compatible: jest.fn().mockResolvedValue(false), + getCurrentNetworkLegacyGasAPICompatibility: jest + .fn() + .mockReturnValue(true), state: { ...buildMockGasFeeStateEthGasPrice(), nonRPCGasFeeApisDisabled: true, @@ -859,7 +874,10 @@ describe('GasFeeController', () => { it('should call determineGasFeeCalculations correctly when nonRPCGasFeeApisDisabled is false', async () => { await setupGasFeeController({ - ...defaultConstructorOptions, + getIsEIP1559Compatible: jest.fn().mockResolvedValue(false), + getCurrentNetworkLegacyGasAPICompatibility: jest + .fn() + .mockReturnValue(true), state: { ...buildMockGasFeeStateEthGasPrice(), nonRPCGasFeeApisDisabled: false, @@ -877,7 +895,10 @@ describe('GasFeeController', () => { it('should call determineGasFeeCalculations correctly when getChainId returns a numeric string input', async () => { await setupGasFeeController({ - ...defaultConstructorOptions, + getIsEIP1559Compatible: jest.fn().mockResolvedValue(false), + getCurrentNetworkLegacyGasAPICompatibility: jest + .fn() + .mockReturnValue(true), getChainId: jest.fn().mockReturnValue('1'), }); @@ -892,9 +913,6 @@ describe('GasFeeController', () => { }); describe('when on any network supporting EIP-1559', () => { - const defaultConstructorOptions = { - getIsEIP1559Compatible: jest.fn().mockResolvedValue(true), - }; const mockDetermineGasFeeCalculations = buildMockGasFeeStateFeeMarket(); beforeEach(() => { @@ -905,7 +923,7 @@ describe('GasFeeController', () => { it('should call determineGasFeeCalculations correctly', async () => { await setupGasFeeController({ - ...defaultConstructorOptions, + getIsEIP1559Compatible: jest.fn().mockResolvedValue(true), networkControllerState: { networkConfigurations: { 'AAAA-BBBB-CCCC-DDDD': { @@ -946,7 +964,9 @@ describe('GasFeeController', () => { }); it('should update the state with a fetched set of estimates', async () => { - await setupGasFeeController(defaultConstructorOptions); + await setupGasFeeController({ + getIsEIP1559Compatible: jest.fn().mockResolvedValue(true), + }); await gasFeeController.fetchGasFeeEstimates(); @@ -956,7 +976,9 @@ describe('GasFeeController', () => { }); it('should return the same data that it puts into state', async () => { - await setupGasFeeController(defaultConstructorOptions); + await setupGasFeeController({ + getIsEIP1559Compatible: jest.fn().mockResolvedValue(true), + }); const estimateData = await gasFeeController.fetchGasFeeEstimates(); @@ -965,7 +987,7 @@ describe('GasFeeController', () => { it('should call determineGasFeeCalculations with a URL that contains the chain ID', async () => { await setupGasFeeController({ - ...defaultConstructorOptions, + getIsEIP1559Compatible: jest.fn().mockResolvedValue(true), getChainId: jest.fn().mockReturnValue('0x1'), }); @@ -979,31 +1001,6 @@ describe('GasFeeController', () => { }); }); describe('when passed a networkClientId in options object', () => { - const defaultConstructorOptions = { - getIsEIP1559Compatible: jest.fn().mockResolvedValue(true), - networkControllerState: { - networksMetadata: { - goerli: { - EIPS: { - 1559: true, - }, - status: NetworkStatus.Available, - }, - sepolia: { - EIPS: { - 1559: true, - }, - status: NetworkStatus.Available, - }, - 'test-network-client-id': { - EIPS: { - 1559: true, - }, - status: NetworkStatus.Available, - }, - }, - }, - }; const mockDetermineGasFeeCalculations = buildMockGasFeeStateFeeMarket(); beforeEach(() => { @@ -1014,7 +1011,29 @@ describe('GasFeeController', () => { it('should call determineGasFeeCalculations correctly', async () => { await setupGasFeeController({ - ...defaultConstructorOptions, + getIsEIP1559Compatible: jest.fn().mockResolvedValue(true), + networkControllerState: { + networksMetadata: { + goerli: { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + sepolia: { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + 'test-network-client-id': { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + }, + }, clientId: '99999', }); @@ -1049,7 +1068,29 @@ describe('GasFeeController', () => { describe("the chainId of the networkClientId matches the globally selected network's chainId", () => { it('should update the globally selected network state with a fetched set of estimates', async () => { await setupGasFeeController({ - ...defaultConstructorOptions, + getIsEIP1559Compatible: jest.fn().mockResolvedValue(true), + networkControllerState: { + networksMetadata: { + goerli: { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + sepolia: { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + 'test-network-client-id': { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + }, + }, getChainId: jest.fn().mockReturnValue(ChainId.goerli), onNetworkDidChange: jest.fn(), }); @@ -1065,7 +1106,29 @@ describe('GasFeeController', () => { it('should update the gasFeeEstimatesByChainId state with a fetched set of estimates', async () => { await setupGasFeeController({ - ...defaultConstructorOptions, + getIsEIP1559Compatible: jest.fn().mockResolvedValue(true), + networkControllerState: { + networksMetadata: { + goerli: { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + sepolia: { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + 'test-network-client-id': { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + }, + }, getChainId: jest.fn().mockReturnValue(ChainId.goerli), onNetworkDidChange: jest.fn(), }); @@ -1083,7 +1146,29 @@ describe('GasFeeController', () => { describe("the chainId of the networkClientId does not match the globally selected network's chainId", () => { it('should not update the globally selected network state with a fetched set of estimates', async () => { await setupGasFeeController({ - ...defaultConstructorOptions, + getIsEIP1559Compatible: jest.fn().mockResolvedValue(true), + networkControllerState: { + networksMetadata: { + goerli: { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + sepolia: { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + 'test-network-client-id': { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + }, + }, getChainId: jest.fn().mockReturnValue(ChainId.mainnet), onNetworkDidChange: jest.fn(), }); @@ -1101,7 +1186,29 @@ describe('GasFeeController', () => { it('should update the gasFeeEstimatesByChainId state with a fetched set of estimates', async () => { await setupGasFeeController({ - ...defaultConstructorOptions, + getIsEIP1559Compatible: jest.fn().mockResolvedValue(true), + networkControllerState: { + networksMetadata: { + goerli: { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + sepolia: { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + 'test-network-client-id': { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + }, + }, getChainId: jest.fn().mockReturnValue(ChainId.mainnet), onNetworkDidChange: jest.fn(), }); @@ -1117,7 +1224,31 @@ describe('GasFeeController', () => { }); it('should return the same data that it puts into state', async () => { - await setupGasFeeController(defaultConstructorOptions); + await setupGasFeeController({ + getIsEIP1559Compatible: jest.fn().mockResolvedValue(true), + networkControllerState: { + networksMetadata: { + goerli: { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + sepolia: { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + 'test-network-client-id': { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + }, + }, + }); const estimateData = await gasFeeController.fetchGasFeeEstimates({ networkClientId: 'sepolia', @@ -1128,7 +1259,29 @@ describe('GasFeeController', () => { it('should call determineGasFeeCalculations with a URL that contains the chain ID', async () => { await setupGasFeeController({ - ...defaultConstructorOptions, + getIsEIP1559Compatible: jest.fn().mockResolvedValue(true), + networkControllerState: { + networksMetadata: { + goerli: { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + sepolia: { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + 'test-network-client-id': { + EIPS: { + 1559: true, + }, + status: NetworkStatus.Available, + }, + }, + }, }); await gasFeeController.fetchGasFeeEstimates({ diff --git a/packages/gas-fee-controller/src/gas-util.test.ts b/packages/gas-fee-controller/src/gas-util.test.ts index b1e5647be8..d6829e7fc7 100644 --- a/packages/gas-fee-controller/src/gas-util.test.ts +++ b/packages/gas-fee-controller/src/gas-util.test.ts @@ -78,10 +78,6 @@ const INFURA_AUTH_TOKEN_MOCK = 'dGVzdDo='; const INFURA_GAS_API_URL_MOCK = 'https://gas.api.infura.io'; describe('gas utils', () => { - beforeEach(() => { - jest.resetAllMocks(); - }); - describe('fetchGasEstimates', () => { it('should fetch external gasFeeEstimates when data is valid', async () => { handleFetchMock.mockResolvedValue(mockEIP1559ApiResponses[0]); diff --git a/packages/logging-controller/src/LoggingController.test.ts b/packages/logging-controller/src/LoggingController.test.ts index 0b8626ef1a..3f092f56b4 100644 --- a/packages/logging-controller/src/LoggingController.test.ts +++ b/packages/logging-controller/src/LoggingController.test.ts @@ -7,10 +7,11 @@ import { LogType } from './logTypes'; import { SigningMethod, SigningStage } from './logTypes/EthSignLog'; jest.mock('uuid', () => { - const actual = jest.requireActual('uuid'); return { - ...actual, - v1: jest.fn(() => actual.v1()), + // We need to use this name as this is what Jest recognizes. + // eslint-disable-next-line @typescript-eslint/naming-convention + __esModule: true, + ...jest.requireActual('uuid'), }; }); @@ -42,9 +43,6 @@ function getRestrictedMessenger( } describe('LoggingController', () => { - afterEach(() => { - jest.clearAllMocks(); - }); it('action: LoggingController:add with generic log', async () => { const unrestricted = getUnrestrictedMessenger(); const messenger = getRestrictedMessenger(unrestricted); @@ -112,6 +110,7 @@ describe('LoggingController', () => { it('action: LoggingController:add prevents possible collision of ids', async () => { const unrestricted = getUnrestrictedMessenger(); const messenger = getRestrictedMessenger(unrestricted); + const uuidV1Spy = jest.spyOn(uuid, 'v1'); const controller = new LoggingController({ messenger, @@ -128,9 +127,7 @@ describe('LoggingController', () => { const { id } = Object.values(controller.state.logs)[0]; - if (jest.isMockFunction(uuid.v1)) { - uuid.v1.mockImplementationOnce(() => id); - } + uuidV1Spy.mockReturnValueOnce(id); expect( // TODO: Either fix this lint violation or explain why it's necessary to ignore. @@ -160,7 +157,7 @@ describe('LoggingController', () => { }), }); - expect(uuid.v1).toHaveBeenCalledTimes(3); + expect(uuidV1Spy).toHaveBeenCalledTimes(3); }); it('internal method: clear', async () => { diff --git a/packages/name-controller/src/NameController.test.ts b/packages/name-controller/src/NameController.test.ts index d05d3fa80a..551d7eb2ff 100644 --- a/packages/name-controller/src/NameController.test.ts +++ b/packages/name-controller/src/NameController.test.ts @@ -34,12 +34,6 @@ const CONTROLLER_ARGS_MOCK = { providers: [], }; -// eslint-disable-next-line jest/prefer-spy-on -console.error = jest.fn(); - -// eslint-disable-next-line jest/prefer-spy-on -Date.now = jest.fn().mockReturnValue(TIME_MOCK * 1000); - /** * Creates a mock name provider. * @@ -76,6 +70,14 @@ function createMockProvider( } describe('NameController', () => { + beforeEach(() => { + jest.spyOn(console, 'error').mockImplementation(() => { + // do nothing + }); + + jest.spyOn(Date, 'now').mockReturnValue(TIME_MOCK * 1000); + }); + describe('setName', () => { it('creates an entry if new%s', () => { const provider1 = createMockProvider(1); diff --git a/packages/name-controller/src/providers/ens.test.ts b/packages/name-controller/src/providers/ens.test.ts index 5166839e60..126fb0cdcd 100644 --- a/packages/name-controller/src/providers/ens.test.ts +++ b/packages/name-controller/src/providers/ens.test.ts @@ -16,10 +16,6 @@ const CONSTRUCTOR_ARGS_MOCK = { } as any; describe('ENSNameProvider', () => { - beforeEach(() => { - jest.resetAllMocks(); - }); - describe('getMetadata', () => { it('returns the provider metadata', () => { const metadata = new ENSNameProvider(CONSTRUCTOR_ARGS_MOCK).getMetadata(); diff --git a/packages/name-controller/src/providers/etherscan.test.ts b/packages/name-controller/src/providers/etherscan.test.ts index dd8d89eff6..78ae2d42e4 100644 --- a/packages/name-controller/src/providers/etherscan.test.ts +++ b/packages/name-controller/src/providers/etherscan.test.ts @@ -14,10 +14,6 @@ const CONTRACT_NAME_2_MOCK = 'TestContractName2'; describe('EtherscanNameProvider', () => { const handleFetchMock = jest.mocked(handleFetch); - beforeEach(() => { - jest.resetAllMocks(); - }); - describe('getMetadata', () => { it('returns the provider metadata', () => { const metadata = new EtherscanNameProvider().getMetadata(); diff --git a/packages/name-controller/src/providers/lens.test.ts b/packages/name-controller/src/providers/lens.test.ts index 729bd504dc..dc22770dbf 100644 --- a/packages/name-controller/src/providers/lens.test.ts +++ b/packages/name-controller/src/providers/lens.test.ts @@ -13,10 +13,6 @@ const HANDLE_2_MOCK = 'TestHandle2'; describe('LensNameProvider', () => { const graphqlMock = jest.mocked(graphQL); - beforeEach(() => { - jest.resetAllMocks(); - }); - describe('getMetadata', () => { it('returns the provider metadata', () => { const metadata = new LensNameProvider().getMetadata(); diff --git a/packages/name-controller/src/providers/token.test.ts b/packages/name-controller/src/providers/token.test.ts index 58bf6c172d..4215e2dfe5 100644 --- a/packages/name-controller/src/providers/token.test.ts +++ b/packages/name-controller/src/providers/token.test.ts @@ -12,10 +12,6 @@ const TOKEN_NAME_MOCK = 'TestTokenName'; describe('TokenNameProvider', () => { const handleFetchMock = jest.mocked(handleFetch); - beforeEach(() => { - jest.resetAllMocks(); - }); - describe('getMetadata', () => { it('returns the provider metadata', () => { const metadata = new TokenNameProvider().getMetadata(); diff --git a/packages/network-controller/tests/NetworkController.test.ts b/packages/network-controller/tests/NetworkController.test.ts index 2b3d924f34..7392a0956a 100644 --- a/packages/network-controller/tests/NetworkController.test.ts +++ b/packages/network-controller/tests/NetworkController.test.ts @@ -153,10 +153,6 @@ const GENERIC_JSON_RPC_ERROR = rpcErrors.internal( ); describe('NetworkController', () => { - beforeEach(() => { - jest.resetAllMocks(); - }); - afterEach(() => { resetAllWhenMocks(); }); diff --git a/packages/permission-controller/src/PermissionController.test.ts b/packages/permission-controller/src/PermissionController.test.ts index 7f117be97a..3595af9285 100644 --- a/packages/permission-controller/src/PermissionController.test.ts +++ b/packages/permission-controller/src/PermissionController.test.ts @@ -774,10 +774,6 @@ function getPermissionMatcher({ } describe('PermissionController', () => { - beforeEach(() => { - jest.clearAllMocks(); - }); - describe('constructor', () => { it('initializes a new PermissionController', () => { const controller = getDefaultPermissionController(); diff --git a/packages/selected-network-controller/tests/SelectedNetworkController.test.ts b/packages/selected-network-controller/tests/SelectedNetworkController.test.ts index cbe603590f..d97564c6bc 100644 --- a/packages/selected-network-controller/tests/SelectedNetworkController.test.ts +++ b/packages/selected-network-controller/tests/SelectedNetworkController.test.ts @@ -203,10 +203,6 @@ const setup = ({ }; describe('SelectedNetworkController', () => { - afterEach(() => { - jest.clearAllMocks(); - }); - describe('constructor', () => { it('can be instantiated with default values', () => { const { controller } = setup(); diff --git a/packages/signature-controller/src/SignatureController.test.ts b/packages/signature-controller/src/SignatureController.test.ts index d4eb1e07c4..1cf29e035c 100644 --- a/packages/signature-controller/src/SignatureController.test.ts +++ b/packages/signature-controller/src/SignatureController.test.ts @@ -173,7 +173,6 @@ describe('SignatureController', () => { }; beforeEach(() => { - jest.resetAllMocks(); jest.spyOn(console, 'info').mockImplementation(() => undefined); addUnapprovedMessageMock.mockResolvedValue(messageIdMock); diff --git a/packages/transaction-controller/src/TransactionController.test.ts b/packages/transaction-controller/src/TransactionController.test.ts index ed7f8b6fb7..f02b63baa1 100644 --- a/packages/transaction-controller/src/TransactionController.test.ts +++ b/packages/transaction-controller/src/TransactionController.test.ts @@ -855,10 +855,6 @@ describe('TransactionController', () => { ); }); - afterEach(() => { - jest.resetAllMocks(); - }); - describe('constructor', () => { it('sets default state', () => { const { controller } = setupController(); diff --git a/packages/transaction-controller/src/gas-flows/LineaGasFeeFlow.test.ts b/packages/transaction-controller/src/gas-flows/LineaGasFeeFlow.test.ts index 154b911bd7..64d84fbdec 100644 --- a/packages/transaction-controller/src/gas-flows/LineaGasFeeFlow.test.ts +++ b/packages/transaction-controller/src/gas-flows/LineaGasFeeFlow.test.ts @@ -60,8 +60,6 @@ describe('LineaGasFeeFlow', () => { let request: GasFeeFlowRequest; beforeEach(() => { - jest.resetAllMocks(); - request = { ethQuery: {} as EthQuery, transactionMeta: TRANSACTION_META_MOCK, diff --git a/packages/transaction-controller/src/helpers/EtherscanRemoteTransactionSource.test.ts b/packages/transaction-controller/src/helpers/EtherscanRemoteTransactionSource.test.ts index cab6442cd6..1c9bc290fd 100644 --- a/packages/transaction-controller/src/helpers/EtherscanRemoteTransactionSource.test.ts +++ b/packages/transaction-controller/src/helpers/EtherscanRemoteTransactionSource.test.ts @@ -44,8 +44,6 @@ describe('EtherscanRemoteTransactionSource', () => { const randomMock = random as jest.MockedFn; beforeEach(() => { - jest.resetAllMocks(); - clock = sinon.useFakeTimers(); fetchEtherscanTransactionsMock.mockResolvedValue( diff --git a/packages/transaction-controller/src/helpers/GasFeePoller.test.ts b/packages/transaction-controller/src/helpers/GasFeePoller.test.ts index 91248f5546..bd62f1fa2d 100644 --- a/packages/transaction-controller/src/helpers/GasFeePoller.test.ts +++ b/packages/transaction-controller/src/helpers/GasFeePoller.test.ts @@ -73,7 +73,6 @@ describe('GasFeePoller', () => { const findNetworkClientIdByChainIdMock = jest.fn(); beforeEach(() => { - jest.resetAllMocks(); jest.clearAllTimers(); gasFeeFlowMock = createGasFeeFlowMock(); diff --git a/packages/transaction-controller/src/helpers/IncomingTransactionHelper.test.ts b/packages/transaction-controller/src/helpers/IncomingTransactionHelper.test.ts index 4afe6c64ed..837a561210 100644 --- a/packages/transaction-controller/src/helpers/IncomingTransactionHelper.test.ts +++ b/packages/transaction-controller/src/helpers/IncomingTransactionHelper.test.ts @@ -129,10 +129,6 @@ async function emitBlockTrackerLatestEvent( } describe('IncomingTransactionHelper', () => { - beforeEach(() => { - jest.resetAllMocks(); - }); - describe('on block tracker latest event', () => { // eslint-disable-next-line jest/expect-expect it('handles errors', async () => { diff --git a/packages/transaction-controller/src/helpers/MultichainTrackingHelper.test.ts b/packages/transaction-controller/src/helpers/MultichainTrackingHelper.test.ts index 69119be8e2..2b5fa6c546 100644 --- a/packages/transaction-controller/src/helpers/MultichainTrackingHelper.test.ts +++ b/packages/transaction-controller/src/helpers/MultichainTrackingHelper.test.ts @@ -221,8 +221,6 @@ function newMultichainTrackingHelper( describe('MultichainTrackingHelper', () => { beforeEach(() => { - jest.resetAllMocks(); - for (const network of [ 'mainnet', 'goerli', diff --git a/packages/transaction-controller/src/helpers/PendingTransactionTracker.test.ts b/packages/transaction-controller/src/helpers/PendingTransactionTracker.test.ts index 0deb17f8ca..920e90d75c 100644 --- a/packages/transaction-controller/src/helpers/PendingTransactionTracker.test.ts +++ b/packages/transaction-controller/src/helpers/PendingTransactionTracker.test.ts @@ -85,8 +85,6 @@ describe('PendingTransactionTracker', () => { } beforeEach(() => { - jest.resetAllMocks(); - blockTracker = createBlockTrackerMock(); failTransaction = jest.fn(); diff --git a/packages/transaction-controller/src/utils/etherscan.test.ts b/packages/transaction-controller/src/utils/etherscan.test.ts index 9a54e575b4..6fd0d7e0ac 100644 --- a/packages/transaction-controller/src/utils/etherscan.test.ts +++ b/packages/transaction-controller/src/utils/etherscan.test.ts @@ -34,10 +34,6 @@ const RESPONSE_MOCK: EtherscanTransactionResponse = { describe('Etherscan', () => { const handleFetchMock = jest.mocked(handleFetch); - beforeEach(() => { - jest.resetAllMocks(); - }); - describe('getEtherscanApiHost', () => { it('returns Etherscan API host for supported network', () => { expect(getEtherscanApiHost(CHAIN_IDS.GOERLI)).toBe( diff --git a/packages/transaction-controller/src/utils/gas.test.ts b/packages/transaction-controller/src/utils/gas.test.ts index 53e66f73e8..b5dfebdf15 100644 --- a/packages/transaction-controller/src/utils/gas.test.ts +++ b/packages/transaction-controller/src/utils/gas.test.ts @@ -93,8 +93,6 @@ describe('gas', () => { } beforeEach(() => { - jest.resetAllMocks(); - updateGasRequest = JSON.parse(JSON.stringify(UPDATE_GAS_REQUEST_MOCK)); }); diff --git a/packages/transaction-controller/src/utils/simulation-api.test.ts b/packages/transaction-controller/src/utils/simulation-api.test.ts index 8ff5603051..aafccb5282 100644 --- a/packages/transaction-controller/src/utils/simulation-api.test.ts +++ b/packages/transaction-controller/src/utils/simulation-api.test.ts @@ -65,8 +65,6 @@ describe('Simulation API Utils', () => { } beforeEach(() => { - jest.resetAllMocks(); - fetchMock = jest.spyOn(global, 'fetch') as jest.MockedFunction< typeof fetch >; diff --git a/packages/transaction-controller/src/utils/simulation.test.ts b/packages/transaction-controller/src/utils/simulation.test.ts index 4d138b51ab..60a742de49 100644 --- a/packages/transaction-controller/src/utils/simulation.test.ts +++ b/packages/transaction-controller/src/utils/simulation.test.ts @@ -266,7 +266,6 @@ describe('Simulation Utils', () => { const simulateTransactionsMock = jest.mocked(simulateTransactions); beforeEach(() => { - jest.resetAllMocks(); jest.spyOn(Interface.prototype, 'encodeFunctionData').mockReturnValue(''); }); diff --git a/packages/transaction-controller/src/utils/utils.test.ts b/packages/transaction-controller/src/utils/utils.test.ts index 14e3374ac4..8d7f1986b0 100644 --- a/packages/transaction-controller/src/utils/utils.test.ts +++ b/packages/transaction-controller/src/utils/utils.test.ts @@ -25,10 +25,6 @@ const TRANSACTION_PARAMS_MOCK: TransactionParams = { }; describe('utils', () => { - afterEach(() => { - jest.clearAllMocks(); - }); - describe('normalizeTransactionParams', () => { it('normalizes properties', () => { const normalized = util.normalizeTransactionParams( diff --git a/packages/transaction-controller/src/utils/validation.test.ts b/packages/transaction-controller/src/utils/validation.test.ts index 265f3e954d..e26175022b 100644 --- a/packages/transaction-controller/src/utils/validation.test.ts +++ b/packages/transaction-controller/src/utils/validation.test.ts @@ -4,10 +4,6 @@ import { TransactionEnvelopeType } from '../types'; import { validateTxParams } from './validation'; describe('validation', () => { - afterEach(() => { - jest.clearAllMocks(); - }); - describe('validateTxParams', () => { it('should throw if no from address', () => { // TODO: Replace `any` with type diff --git a/packages/user-operation-controller/src/UserOperationController.test.ts b/packages/user-operation-controller/src/UserOperationController.test.ts index 6e9554d695..e41af0e540 100644 --- a/packages/user-operation-controller/src/UserOperationController.test.ts +++ b/packages/user-operation-controller/src/UserOperationController.test.ts @@ -192,8 +192,6 @@ describe('UserOperationController', () => { ); beforeEach(() => { - jest.resetAllMocks(); - jest.spyOn(BundlerHelper, 'Bundler').mockReturnValue(bundlerMock); jest .spyOn(PendingUserOperationTrackerHelper, 'PendingUserOperationTracker') diff --git a/packages/user-operation-controller/src/helpers/PendingUserOperationTracker.test.ts b/packages/user-operation-controller/src/helpers/PendingUserOperationTracker.test.ts index b3c16aa358..2285f2cd90 100644 --- a/packages/user-operation-controller/src/helpers/PendingUserOperationTracker.test.ts +++ b/packages/user-operation-controller/src/helpers/PendingUserOperationTracker.test.ts @@ -121,7 +121,6 @@ describe('PendingUserOperationTracker', () => { } beforeEach(() => { - jest.resetAllMocks(); jest.spyOn(BundlerHelper, 'Bundler').mockReturnValue(bundlerMock); messengerMock.call.mockReturnValue({ diff --git a/packages/user-operation-controller/src/helpers/SnapSmartContractAccount.test.ts b/packages/user-operation-controller/src/helpers/SnapSmartContractAccount.test.ts index 960ba8b718..11475aa038 100644 --- a/packages/user-operation-controller/src/helpers/SnapSmartContractAccount.test.ts +++ b/packages/user-operation-controller/src/helpers/SnapSmartContractAccount.test.ts @@ -89,8 +89,6 @@ describe('SnapSmartContractAccount', () => { let signMock: jest.MockedFn; beforeEach(() => { - jest.resetAllMocks(); - messengerMock = createMessengerMock(); prepareMock = jest.fn(); patchMock = jest.fn(); diff --git a/packages/user-operation-controller/src/utils/gas-fees.test.ts b/packages/user-operation-controller/src/utils/gas-fees.test.ts index 3d7e1fb566..25cb1b1a0d 100644 --- a/packages/user-operation-controller/src/utils/gas-fees.test.ts +++ b/packages/user-operation-controller/src/utils/gas-fees.test.ts @@ -34,8 +34,6 @@ describe('gas-fees', () => { let request: jest.Mocked; beforeEach(() => { - jest.resetAllMocks(); - request = cloneDeep(UPDATE_GAS_FEES_REQUEST_MOCK); jest diff --git a/scripts/create-package/commands.test.ts b/scripts/create-package/commands.test.ts index 97153222fb..e557dd5150 100644 --- a/scripts/create-package/commands.test.ts +++ b/scripts/create-package/commands.test.ts @@ -13,10 +13,6 @@ jest.mock('./utils', () => ({ jest.useFakeTimers().setSystemTime(new Date('2023-01-02')); describe('create-package/commands', () => { - afterEach(() => { - jest.resetAllMocks(); - }); - describe('createPackageHandler', () => { it('should create the expected package', async () => { (utils.readMonorepoFiles as jest.Mock).mockResolvedValue({ diff --git a/scripts/create-package/utils.test.ts b/scripts/create-package/utils.test.ts index 263b52f2ec..af903f8ede 100644 --- a/scripts/create-package/utils.test.ts +++ b/scripts/create-package/utils.test.ts @@ -29,10 +29,6 @@ jest.mock('./fs-utils', () => ({ })); describe('create-package/utils', () => { - afterEach(() => { - jest.resetAllMocks(); - }); - describe('readMonorepoFiles', () => { const tsConfig = JSON.stringify({ references: [{ path: '../packages/foo' }],