Skip to content

Commit 1e87559

Browse files
authored
test: fix mocha vs jest type conflicts in test files (#31049)
## **Description** This PR resolves conflicting types between jest and mocha that were causing [issues with the type of "it"](https://github.com/search?q=org%3AMetaMask%20repo%3AMetaMask%2Fmetamask-extension%20it.each%20&type=code) in unit tests which were masking type errors. It also fixes type issues that were masked by previously disregarding type errors. In particular, this PR creates an [isolated TypeScript configuration for the e2e tests](https://github.com/MetaMask/metamask-extension/pull/31049/files#diff-bfc1581736f7b790e103572bf8e349eab3346aaa0d29b505f8f9707bfd603e7e) where they get their own type checking context. Since mocha is only needed in e2e tests, Mocha types will not be considered for files outside of that folder. <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/31049?quickstart=1) ## **Related issues** Fixes: ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
1 parent e83da5d commit 1e87559

File tree

48 files changed

+139
-205
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+139
-205
lines changed

app/scripts/controllers/bridge-status/validators.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ const BridgeTxStatusResponses = {
218218

219219
describe('validators', () => {
220220
describe('bridgeStatusValidator', () => {
221-
// @ts-expect-error - it.each is a function
222221
it.each([
223222
{
224223
input: BridgeTxStatusResponses.STATUS_PENDING_VALID,

app/scripts/controllers/encryption-public-key.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ describe('EncryptionPublicKeyController', () => {
191191
});
192192

193193
describe('newRequestEncryptionPublicKey', () => {
194-
// @ts-expect-error This function is missing from the Mocha type definitions
195194
it.each([
196195
['Ledger', KeyringType.ledger],
197196
['Trezor', KeyringType.trezor],

app/scripts/controllers/metametrics-controller.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,6 @@ describe('MetaMetricsController', function () {
873873
});
874874

875875
describe('Change Signature XXX anonymous event names', function () {
876-
// @ts-expect-error This function is missing from the Mocha type definitions
877876
it.each([
878877
['Signature Requested', 'Signature Requested Anon'],
879878
['Signature Rejected', 'Signature Rejected Anon'],

app/scripts/lib/AccountIdentitiesPetnamesBridge.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ describe('AccountIdentitiesPetnamesBridge', () => {
204204
});
205205

206206
describe('shouldSyncPetname', () => {
207-
// @ts-expect-error This is missing from the Mocha type definitions
208207
it.each([
209208
{
210209
origin: NameOrigin.ACCOUNT_IDENTITY,

app/scripts/lib/approval/utils.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ describe('Approval Utils', () => {
4848
);
4949
});
5050

51-
// @ts-expect-error This function is missing from the Mocha type definitions
5251
it.each([
5352
ApprovalType.SnapDialogAlert,
5453
ApprovalType.SnapDialogPrompt,
@@ -64,7 +63,6 @@ describe('Approval Utils', () => {
6463
expect(approvalController.accept).toHaveBeenCalledWith(ID_MOCK, null);
6564
});
6665

67-
// @ts-expect-error This function is missing from the Mocha type definitions
6866
it.each([
6967
ApprovalType.SnapDialogConfirmation,
7068
SNAP_MANAGE_ACCOUNTS_CONFIRMATION_TYPES.confirmAccountCreation,
@@ -81,7 +79,6 @@ describe('Approval Utils', () => {
8179
expect(approvalController.accept).toHaveBeenCalledWith(ID_MOCK, false);
8280
});
8381

84-
// @ts-expect-error This function is missing from the Mocha type definitions
8582
it.each([
8683
ApprovalType.SnapDialogAlert,
8784
ApprovalType.SnapDialogPrompt,

app/scripts/lib/createEvmMethodsToNonEvmAccountReqFilterMiddleware.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import createEvmMethodsToNonEvmAccountReqFilterMiddleware, {
55
} from './createEvmMethodsToNonEvmAccountReqFilterMiddleware';
66

77
describe('createEvmMethodsToNonEvmAccountReqFilterMiddleware', () => {
8-
const getMockRequest = (method: string, params: Record<string, Json>) => ({
8+
const getMockRequest = (
9+
method: string,
10+
params: Record<string, Json>[] | undefined,
11+
) => ({
912
jsonrpc: jsonrpc2,
1013
id: 1,
1114
method,
1215
params,
1316
});
1417
const getMockResponse = () => ({ jsonrpc: jsonrpc2, id: 'foo' });
1518

16-
// @ts-expect-error This function is missing from the Mocha type definitions
1719
it.each([
1820
// EVM requests
1921
{
@@ -285,8 +287,8 @@ describe('createEvmMethodsToNonEvmAccountReqFilterMiddleware', () => {
285287
}: {
286288
accountType: EthAccountType | BtcAccountType;
287289
method: string;
288-
params: Record<string, Json>;
289-
calledNext: number;
290+
params: Record<string, Json>[] | undefined;
291+
calledNext: boolean;
290292
}) => {
291293
const filterFn = createEvmMethodsToNonEvmAccountReqFilterMiddleware({
292294
messenger: {

app/scripts/lib/multichain/address.test.ts

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,91 +4,73 @@ import {
44
normalizeSafeAddress,
55
} from './address';
66

7-
type TestAddress = {
8-
address: string;
9-
normalizedAddress: string;
10-
checksumAddress: string;
11-
};
12-
13-
const ETH_ADDRESSES = [
14-
// Lower-case address
15-
{
16-
address: '0x6431726eee67570bf6f0cf892ae0a3988f03903f',
17-
normalizedAddress: '0x6431726eee67570bf6f0cf892ae0a3988f03903f',
18-
checksumAddress: '0x6431726EEE67570BF6f0Cf892aE0a3988F03903F',
19-
},
20-
// Checksum address
21-
{
22-
address: '0x6431726EEE67570BF6f0Cf892aE0a3988F03903F',
23-
normalizedAddress: '0x6431726eee67570bf6f0cf892ae0a3988f03903f',
24-
checksumAddress: '0x6431726EEE67570BF6f0Cf892aE0a3988F03903F',
25-
},
26-
];
7+
describe('address', () => {
8+
const TEST_CASES_EVM_ADDRESSES = [
9+
{
10+
address: '0x6431726eee67570bf6f0cf892ae0a3988f03903f', // Lower-case address
11+
normalizedAddress: '0x6431726eee67570bf6f0cf892ae0a3988f03903f',
12+
checksumAddress: '0x6431726EEE67570BF6f0Cf892aE0a3988F03903F',
13+
},
14+
{
15+
address: '0x6431726EEE67570BF6f0Cf892aE0a3988F03903F', // Checksum address
16+
normalizedAddress: '0x6431726eee67570bf6f0cf892ae0a3988f03903f',
17+
checksumAddress: '0x6431726EEE67570BF6f0Cf892aE0a3988F03903F',
18+
},
19+
];
2720

28-
const NON_EVM_ADDRESSES = [
29-
{
30-
address: '0xdeadbeef',
31-
},
32-
{
33-
address: 'bc1ql49ydapnjafl5t2cp9zqpjwe6pdgmxy98859v2',
34-
},
35-
];
21+
const TEST_CASES_NON_EVM_ADDRESSES = [
22+
'0xdeadbeef',
23+
'bc1ql49ydapnjafl5t2cp9zqpjwe6pdgmxy98859v2',
24+
];
3625

37-
describe('address', () => {
3826
describe('isEthAddress', () => {
39-
// @ts-expect-error This is missing from the Mocha type definitions
40-
it.each(ETH_ADDRESSES)(
27+
it.each(TEST_CASES_EVM_ADDRESSES)(
4128
'returns true if address is an ethereum address: $address',
42-
({ address }: TestAddress) => {
29+
({ address }) => {
4330
expect(isEthAddress(address)).toBe(true);
4431
expect(isEthAddress(address.toLowerCase())).toBe(true);
4532
},
4633
);
4734

48-
// @ts-expect-error This is missing from the Mocha type definitions
49-
it.each(NON_EVM_ADDRESSES)(
35+
it.each(TEST_CASES_NON_EVM_ADDRESSES)(
5036
'returns false if address is not an ethereum address: $address',
51-
({ address }: TestAddress) => {
37+
(address) => {
5238
expect(isEthAddress(address)).toBe(false);
5339
},
5440
);
5541
});
5642

5743
describe('normalizeAddress', () => {
58-
// @ts-expect-error This is missing from the Mocha type definitions
59-
it.each(ETH_ADDRESSES)(
44+
it.each(TEST_CASES_EVM_ADDRESSES)(
6045
'normalizes address: $address',
61-
({ address, normalizedAddress }: TestAddress) => {
46+
({ address, normalizedAddress }) => {
6247
expect(normalizeAddress(address)).toBe(normalizedAddress);
6348
expect(normalizeAddress(address.toLowerCase())).toBe(normalizedAddress);
6449
},
6550
);
6651

67-
// @ts-expect-error This is missing from the Mocha type definitions
68-
it.each(NON_EVM_ADDRESSES)(
52+
it.each(TEST_CASES_NON_EVM_ADDRESSES)(
6953
'returns the original address if its a non-EVM address',
70-
({ address }: TestAddress) => {
54+
(address) => {
7155
expect(normalizeAddress(address)).toBe(address);
7256
},
7357
);
7458
});
7559

7660
describe('normalizeSafeAddress', () => {
77-
// @ts-expect-error This is missing from the Mocha type definitions
78-
it.each(ETH_ADDRESSES)(
61+
it.each(TEST_CASES_EVM_ADDRESSES)(
7962
'normalizes address to its "safe" form: $address to: $checksumAddress',
80-
({ address, checksumAddress }: TestAddress) => {
63+
({ address, checksumAddress }) => {
8164
expect(normalizeSafeAddress(address)).toBe(checksumAddress);
8265
expect(normalizeSafeAddress(address.toLowerCase())).toBe(
8366
checksumAddress,
8467
);
8568
},
8669
);
8770

88-
// @ts-expect-error This is missing from the Mocha type definitions
89-
it.each(NON_EVM_ADDRESSES)(
71+
it.each(TEST_CASES_NON_EVM_ADDRESSES)(
9072
'returns the original address if its a non-EVM address',
91-
({ address }: TestAddress) => {
73+
(address) => {
9274
expect(normalizeSafeAddress(address)).toBe(address);
9375
},
9476
);

app/scripts/lib/ppom/ppom-util.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ describe('PPOM Utils', () => {
261261
);
262262
});
263263

264-
// @ts-expect-error This is missing from the Mocha type definitions
265264
it.each([METHOD_SIGN_TYPED_DATA_V3, METHOD_SIGN_TYPED_DATA_V4])(
266265
'sanitizes request params if method is %s',
267266
async (method: string) => {

app/scripts/lib/rpc-method-middleware/createUnsupportedMethodMiddleware.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ describe('createUnsupportedMethodMiddleware', () => {
2121
expect(endMock).not.toHaveBeenCalled();
2222
});
2323

24-
// @ts-expect-error This function is missing from the Mocha type definitions
2524
it.each([...UNSUPPORTED_RPC_METHODS])(
2625
'ends requests for default unsupported rpc methods when no list is provided: %s',
2726
(method: string) => {
@@ -40,7 +39,6 @@ describe('createUnsupportedMethodMiddleware', () => {
4039

4140
const unsupportedMethods = new Set(['foo', 'bar']);
4241

43-
// @ts-expect-error This function is missing from the Mocha type definitions
4442
it.each([...unsupportedMethods])(
4543
'ends requests for methods that are in the provided list of unsupported methods: %s',
4644
(method: string) => {

app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ describe('Ethereum Chain Utils', () => {
253253
});
254254
});
255255

256-
// @ts-expect-error This function is missing from the Mocha type definitions
257256
describe.each([
258257
['legacy', false],
259258
['multichain', true],

0 commit comments

Comments
 (0)