Skip to content

Commit 2b1841c

Browse files
mcmirejiexiadonesky1Prithpal-Sooriyashanejonas
authored
Enable resetMocks Jest configuration option (#4417)
## Explanation <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> 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 <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? For example: * Fixes #12345 * Related to #67890 --> Fixes #745. ## Changelog <!-- If you're making any consumer-facing changes, list those changes here as if you were updating a changelog, using the template below as a guide. (CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or FIXED. For security-related issues, follow the Security Advisory process.) Please take care to name the exact pieces of the API you've added or changed (e.g. types, interfaces, functions, or methods). If there are any breaking changes, make sure to offer a solution for consumers to follow once they upgrade to the changes. Finally, if you're only making changes to development scripts or tests, you may replace the template below with "None". --> (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 <jiexiluan@gmail.com> Co-authored-by: Alex Donesky <adonesky@gmail.com> Co-authored-by: Prithpal Sooriya <prithpal.sooriya@consensys.net> Co-authored-by: Shane <jonas.shane@gmail.com> Co-authored-by: Derek Brans <dbrans@gmail.com> Co-authored-by: Monte Lai <monte.lai@consensys.net>
1 parent ca683e8 commit 2b1841c

40 files changed

+232
-181
lines changed

jest.config.packages.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ module.exports = {
108108

109109
// "resetMocks" resets all mocks, including mocked modules, to jest.fn(),
110110
// between each test case.
111-
// TODO: Enable
112-
// resetMocks: true,
111+
resetMocks: true,
113112

114113
// Reset the module registry before running each individual test
115114
// resetModules: false,

jest.config.scripts.js

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ module.exports = {
5050
// // A preset that is used as a base for Jest's configuration
5151
// preset: 'ts-jest',
5252

53+
// "resetMocks" resets all mocks, including mocked modules, to jest.fn(),
54+
// between each test case.
55+
resetMocks: true,
56+
5357
// "restoreMocks" restores all mocks created using jest.spyOn to their
5458
// original implementations, between each test. It does not affect mocked
5559
// modules.

packages/accounts-controller/src/AccountsController.test.ts

-11
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,6 @@ function setupAccountsController({
315315
}
316316

317317
describe('AccountsController', () => {
318-
afterEach(() => {
319-
jest.resetAllMocks();
320-
});
321-
322318
describe('onSnapStateChange', () => {
323319
it('be used enable an account if the Snap is enabled and not blocked', async () => {
324320
const messenger = buildMessenger();
@@ -449,9 +445,6 @@ describe('AccountsController', () => {
449445
});
450446

451447
describe('onKeyringStateChange', () => {
452-
afterEach(() => {
453-
jest.clearAllMocks();
454-
});
455448
it('not update state when only keyring is unlocked without any keyrings', async () => {
456449
const messenger = buildMessenger();
457450
const { accountsController } = setupAccountsController({
@@ -1304,10 +1297,6 @@ describe('AccountsController', () => {
13041297
);
13051298
});
13061299

1307-
afterEach(() => {
1308-
jest.clearAllMocks();
1309-
});
1310-
13111300
it('update accounts with normal accounts', async () => {
13121301
mockUUID.mockReturnValueOnce('mock-id').mockReturnValueOnce('mock-id2');
13131302
const messenger = buildMessenger();

packages/approval-controller/src/ApprovalController.test.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { ControllerMessenger } from '@metamask/base-controller';
44
import { errorCodes, JsonRpcError } from '@metamask/rpc-errors';
5+
import { nanoid } from 'nanoid';
56

67
import type {
78
AddApprovalOptions,
@@ -24,9 +25,9 @@ import {
2425
NoApprovalFlowsError,
2526
} from './errors';
2627

27-
jest.mock('nanoid', () => ({
28-
nanoid: jest.fn(() => 'TestId'),
29-
}));
28+
jest.mock('nanoid');
29+
30+
const nanoidMock = jest.mocked(nanoid);
3031

3132
const PENDING_APPROVALS_STORE_KEY = 'pendingApprovals';
3233
const APPROVAL_FLOWS_STORE_KEY = 'approvalFlows';
@@ -243,6 +244,7 @@ describe('approval controller', () => {
243244
let showApprovalRequest: jest.Mock;
244245

245246
beforeEach(() => {
247+
nanoidMock.mockReturnValue('TestId');
246248
jest.spyOn(global.console, 'info').mockImplementation(() => undefined);
247249

248250
showApprovalRequest = jest.fn();

packages/assets-controllers/src/CurrencyRateController.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ describe('CurrencyRateController', () => {
7575

7676
afterEach(() => {
7777
clock.restore();
78-
jest.restoreAllMocks();
7978
});
8079

8180
it('should set default state', () => {

packages/assets-controllers/src/RatesController/RatesController.test.ts

-6
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ function setupRatesController({
9090
describe('RatesController', () => {
9191
let clock: sinon.SinonFakeTimers;
9292

93-
afterEach(() => {
94-
jest.resetAllMocks();
95-
});
96-
9793
describe('construct', () => {
9894
it('constructs the RatesController with default values', () => {
9995
const ratesController = setupRatesController({
@@ -116,7 +112,6 @@ describe('RatesController', () => {
116112

117113
afterEach(() => {
118114
clock.restore();
119-
jest.restoreAllMocks();
120115
});
121116

122117
it('starts the polling process with default values', async () => {
@@ -249,7 +244,6 @@ describe('RatesController', () => {
249244

250245
afterEach(() => {
251246
clock.restore();
252-
jest.restoreAllMocks();
253247
});
254248

255249
it('stops the polling process', async () => {

packages/assets-controllers/src/TokenListController.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,6 @@ const getRestrictedMessenger = (
517517

518518
describe('TokenListController', () => {
519519
afterEach(() => {
520-
jest.restoreAllMocks();
521520
jest.clearAllTimers();
522521
sinon.restore();
523522
});

packages/assets-controllers/src/TokenRatesController.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ function buildTokenRatesControllerMessenger(
8181
}
8282

8383
describe('TokenRatesController', () => {
84-
afterEach(() => {
85-
jest.restoreAllMocks();
86-
});
87-
8884
describe('constructor', () => {
8985
let clock: sinon.SinonFakeTimers;
9086

packages/assets-controllers/src/TokensController.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ describe('TokensController', () => {
7777

7878
afterEach(() => {
7979
sinon.restore();
80-
jest.resetAllMocks();
8180
});
8281

8382
it('should set default state', async () => {

0 commit comments

Comments
 (0)