Skip to content

Commit

Permalink
Enable resetMocks Jest configuration option (#4417)
Browse files Browse the repository at this point in the history
## 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>
  • Loading branch information
7 people committed Jun 24, 2024
1 parent ca683e8 commit 2b1841c
Show file tree
Hide file tree
Showing 40 changed files with 232 additions and 181 deletions.
3 changes: 1 addition & 2 deletions jest.config.packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions jest.config.scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 0 additions & 11 deletions packages/accounts-controller/src/AccountsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 5 additions & 3 deletions packages/approval-controller/src/ApprovalController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { ControllerMessenger } from '@metamask/base-controller';
import { errorCodes, JsonRpcError } from '@metamask/rpc-errors';
import { nanoid } from 'nanoid';

import type {
AddApprovalOptions,
Expand All @@ -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';
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ describe('CurrencyRateController', () => {

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

it('should set default state', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -116,7 +112,6 @@ describe('RatesController', () => {

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

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

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

it('stops the polling process', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,6 @@ const getRestrictedMessenger = (

describe('TokenListController', () => {
afterEach(() => {
jest.restoreAllMocks();
jest.clearAllTimers();
sinon.restore();
});
Expand Down
4 changes: 0 additions & 4 deletions packages/assets-controllers/src/TokenRatesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ function buildTokenRatesControllerMessenger(
}

describe('TokenRatesController', () => {
afterEach(() => {
jest.restoreAllMocks();
});

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

Expand Down
1 change: 0 additions & 1 deletion packages/assets-controllers/src/TokensController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ describe('TokensController', () => {

afterEach(() => {
sinon.restore();
jest.resetAllMocks();
});

it('should set default state', async () => {
Expand Down
Loading

0 comments on commit 2b1841c

Please sign in to comment.