Skip to content

Commit 872cdaf

Browse files
test: MMQA-1556 - consolidate network names and chainIds in E2E constants (#47778)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** E2E specs repeated network chain ids and display names as local string literals, and the `NetworkId` enum lived inside the `SelectNetworkModal` page object file even though the class never used it. This refactor consolidates them in one E2E helper module, [`test/e2e/helpers/network-constants.ts`](test/e2e/helpers/network-constants.ts): - `NetworkId`: CAIP-2 chain ids (including `LOCALHOST`, `ZKSYNC_ERA` and `SOLANA`), EVM ones derived from the shared `CHAIN_IDS` via `toEvmCaipChainId` and Solana from `MultichainNetworks`, instead of hand-typed `eip155:*` strings. It is a `const` object (a TS enum cannot call a function), and the same-named type is exported. - `NetworkName`: built-in network display names (Ethereum, Sepolia, Arbitrum, Avalanche, Linea, Localhost, Bitcoin, Tron), taken from the `*_DISPLAY_NAME` constants and `MULTICHAIN_NETWORK_TO_NICKNAME` in `shared/constants`. Specs that defined their own `'Ethereum'` / `'Sepolia'` / `'Bitcoin'` / `'Tron'` constants now use `NetworkName`. The values are identical, so there is no behavior change. A colocated unit test pins every value. Behavior impact: none. Test-only change. Files changed: - `test/e2e/helpers/network-constants.ts` (new) - `test/e2e/helpers/network-constants.test.ts` (new) - `test/e2e/page-objects/pages/networks/select-network-modal.ts` (removed `NetworkId` enum) - `test/e2e/tests/request-queuing/ui.spec.ts`, `tokens/import-tokens.spec.ts`, `tokens/nft/filter-nfts.spec.ts`, `network/network-manager.spec.ts` (import `NetworkId` from the new module) - `test/e2e/tests/multichain/aggregated-balances.spec.ts`, `multichain/asset-list.spec.ts`, `network/is-present-bitcoin.spec.ts`, `network/is-present-tron.spec.ts` (use `NetworkName`) - `test/e2e/tests/network/multi-rpc.spec.ts`, `update-network.spec.ts`, `custom-rpc-history.spec.ts`, `network-manager.spec.ts`, `linea-discover-button.spec.ts`, `switch-network.spec.ts` (hardcoded `eip155:*` ids and network names replaced with `NetworkId` / `NetworkName`) - `test/e2e/tests/mm-connect/connect-multichain.spec.ts` (expected `eth_chainId` hex values use `CHAIN_IDS`) - `test/e2e/tests/network/popular-network.spec.ts` (Avalanche/Arbitrum hex ids, CAIP id and display names now come from `CHAIN_IDS`, `NetworkId` and `NetworkName`) - `test/e2e/constants.ts` (`SOLANA_MAINNET_SCOPE`, `SOLANA_MAINNET_CHAIN_ID`, `MM_CONNECT_EVM_CHAINS`, `MM_CONNECT_FEATURED_CHAIN_IDS` and the mock `chain_id` are built from `NetworkId` / `CHAIN_IDS` instead of hand-typed strings; exported names and values are unchanged) Not changed: `TRON_NILE_NAME` / `TRON_SHASTA_NAME` (shared nicknames differ), custom/fake network names, and other hardcoded chain ids (status codes, asset ids, custom-network harness). ## **Changelog** CHANGELOG entry: null ## **Related issues** Fixes: MMQA-1556 ## **Manual testing steps** N/A: this is a structural move in E2E test code with no product or UI change. The moved values are pinned by `yarn test:unit test/e2e/helpers/network-constants.test.ts`. ## **Screenshots/Recordings** ### **Before** No UI change. Ticket context: the Linear-style ticket header shows parent MMQA-325 and the checked child MMQA-1556, "Consolidate network name, chainIds in constants", with an empty description. Before this change, network names and chain ids were repeated as local literals across E2E specs, and the `NetworkId` enum lived in the `SelectNetworkModal` page object. ### **After** No UI change. Network names and chain ids are defined once in `test/e2e/helpers/network-constants.ts` and imported by the specs. ## **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. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Test-only refactor with values pinned by unit tests; no production code or runtime behavior changes. > > **Overview** > E2E tests no longer repeat CAIP-2 chain IDs and network display names as literals. A new **`test/e2e/helpers/network-constants.ts`** module centralizes **`NetworkId`** (derived from shared `CHAIN_IDS` / `MultichainNetworks`) and **`NetworkName`** (from product display-name constants), with a unit test that locks the expected values. > > The **`NetworkId` enum is removed** from `select-network-modal.ts`; specs and **`test/e2e/constants.ts`** now import these helpers instead of hardcoded `eip155:*` strings, hex chain IDs, or labels like `'Ethereum'`. MM Connect and analytics mock constants are wired through the same shared sources so E2E identifiers stay aligned with the extension. > > **Behavior is unchanged**—this is structural test maintenance only. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 805583e. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent f0ab05f commit 872cdaf

19 files changed

Lines changed: 162 additions & 70 deletions

‎test/e2e/constants.ts‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import { parseCaipChainId } from '@metamask/utils';
2+
import { CHAIN_IDS } from '../../shared/constants/chain-ids';
13
import defaultFixtureJson from './fixtures/default-fixture.json';
4+
import { NetworkId } from './helpers/network-constants';
25

36
/** Persisted versioned-data version for fixtures. */
47
export const FIXTURE_STATE_METADATA_VERSION = defaultFixtureJson.meta.version;
@@ -268,34 +271,36 @@ export const DEFAULT_FIXTURE_SOLANA_ACCOUNT =
268271
'4tE76eixEgyJDrdykdWJR1XBkzUk4cLMvqjR2xVJUxer';
269272

270273
/* Solana Mainnet chain ID (genesis hash) */
271-
export const SOLANA_MAINNET_CHAIN_ID = '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';
274+
export const SOLANA_MAINNET_CHAIN_ID = parseCaipChainId(
275+
NetworkId.SOLANA,
276+
).reference;
272277

273278
/* Solana Mainnet scope in CAIP-2 format */
274-
export const SOLANA_MAINNET_SCOPE = `solana:${SOLANA_MAINNET_CHAIN_ID}`;
279+
export const SOLANA_MAINNET_SCOPE = NetworkId.SOLANA;
275280

276281
/* CAIP-2 EVM chain scopes used in MM Connect Multichain tests.
277282
* On localhost the playground pre-checks eip155:1337, so we use that
278283
* instead of eip155:1 to avoid unchecking it (which triggers a React
279284
* stale-closure race in handleCheckboxChange). */
280285
export const MM_CONNECT_EVM_CHAINS = {
281-
LOCALHOST: 'eip155:1337',
282-
POLYGON: 'eip155:137',
283-
LINEA: 'eip155:59144',
286+
LOCALHOST: NetworkId.LOCALHOST,
287+
POLYGON: NetworkId.POLYGON,
288+
LINEA: NetworkId.LINEA,
284289
} as const;
285290

286291
/* All featured CAIP-2 chain IDs available in the MM Connect playground */
287292
export const MM_CONNECT_FEATURED_CHAIN_IDS = [
288-
'eip155:1337', // Localhost 8545 (pre-checked on page load in local E2E env)
289-
'eip155:1', // Ethereum Mainnet
290-
'eip155:59144', // Linea Mainnet
291-
'eip155:42161', // Arbitrum One
292-
'eip155:43114', // Avalanche C-Chain
293-
'eip155:56', // BNB Chain
294-
'eip155:10', // OP Mainnet
295-
'eip155:137', // Polygon Mainnet
296-
'eip155:324', // zkSync Era
297-
'eip155:8453', // Base Mainnet
298-
`solana:${SOLANA_MAINNET_CHAIN_ID}`, // Solana Mainnet
293+
NetworkId.LOCALHOST, // Localhost 8545 (pre-checked on page load in local E2E env)
294+
NetworkId.ETHEREUM, // Ethereum Mainnet
295+
NetworkId.LINEA, // Linea Mainnet
296+
NetworkId.ARBITRUM, // Arbitrum One
297+
NetworkId.AVALANCHE, // Avalanche C-Chain
298+
NetworkId.BSC, // BNB Chain
299+
NetworkId.OPTIMISM, // OP Mainnet
300+
NetworkId.POLYGON, // Polygon Mainnet
301+
NetworkId.ZKSYNC_ERA, // zkSync Era
302+
NetworkId.BASE, // Base Mainnet
303+
NetworkId.SOLANA, // Solana Mainnet
299304
] as const;
300305

301306
/* Default (mocked) SOLANA balance used by the Solana RPC provider */
@@ -379,7 +384,7 @@ export const MOCK_DOWNSTREAM_EVENT_ENRICHMENT_PROPERTIES = {
379384
locale: 'en',
380385
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
381386
// eslint-disable-next-line @typescript-eslint/naming-convention
382-
chain_id: '0x539',
387+
chain_id: CHAIN_IDS.LOCALHOST,
383388
...MOCK_PROFILE_IDENTITY_EVENT_PROPERTIES,
384389
} as const;
385390

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { NetworkId, NetworkName } from './network-constants';
2+
3+
describe('network-constants', () => {
4+
describe('NetworkId', () => {
5+
it('maps each network to its CAIP-2 chain id', () => {
6+
expect(NetworkId).toStrictEqual({
7+
ARBITRUM: 'eip155:42161',
8+
AVALANCHE: 'eip155:43114',
9+
BASE: 'eip155:8453',
10+
BSC: 'eip155:56',
11+
ETHEREUM: 'eip155:1',
12+
LINEA: 'eip155:59144',
13+
LOCALHOST: 'eip155:1337',
14+
OPTIMISM: 'eip155:10',
15+
POLYGON: 'eip155:137',
16+
SOLANA: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
17+
XDC: 'eip155:50',
18+
ZKSYNC_ERA: 'eip155:324',
19+
});
20+
});
21+
});
22+
23+
describe('NetworkName', () => {
24+
it('maps each network to the display name shown in the UI', () => {
25+
expect(NetworkName).toStrictEqual({
26+
ARBITRUM: 'Arbitrum',
27+
AVALANCHE: 'Avalanche',
28+
BITCOIN: 'Bitcoin',
29+
ETHEREUM: 'Ethereum',
30+
LINEA: 'Linea',
31+
LOCALHOST: 'Localhost 8545',
32+
SEPOLIA: 'Sepolia',
33+
TRON: 'Tron',
34+
});
35+
});
36+
});
37+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { toEvmCaipChainId } from '@metamask/multichain-network-controller';
2+
import { CHAIN_IDS } from '../../../shared/constants/chain-ids';
3+
import {
4+
ARBITRUM_DISPLAY_NAME,
5+
AVALANCHE_DISPLAY_NAME,
6+
LINEA_MAINNET_DISPLAY_NAME,
7+
LOCALHOST_DISPLAY_NAME,
8+
MAINNET_DISPLAY_NAME,
9+
SEPOLIA_DISPLAY_NAME,
10+
} from '../../../shared/constants/network';
11+
import {
12+
MULTICHAIN_NETWORK_TO_NICKNAME,
13+
MultichainNetworks,
14+
} from '../../../shared/constants/multichain/networks';
15+
16+
/**
17+
* CAIP-2 chain ids of the networks the E2E tests refer to. EVM values are
18+
* derived from the shared hex `CHAIN_IDS` and non-EVM values from
19+
* `MultichainNetworks`, so they cannot drift from the product.
20+
*/
21+
export const NetworkId = {
22+
ARBITRUM: toEvmCaipChainId(CHAIN_IDS.ARBITRUM),
23+
AVALANCHE: toEvmCaipChainId(CHAIN_IDS.AVALANCHE),
24+
BASE: toEvmCaipChainId(CHAIN_IDS.BASE),
25+
BSC: toEvmCaipChainId(CHAIN_IDS.BSC),
26+
ETHEREUM: toEvmCaipChainId(CHAIN_IDS.MAINNET),
27+
LINEA: toEvmCaipChainId(CHAIN_IDS.LINEA_MAINNET),
28+
LOCALHOST: toEvmCaipChainId(CHAIN_IDS.LOCALHOST),
29+
OPTIMISM: toEvmCaipChainId(CHAIN_IDS.OPTIMISM),
30+
POLYGON: toEvmCaipChainId(CHAIN_IDS.POLYGON),
31+
SOLANA: MultichainNetworks.SOLANA,
32+
XDC: toEvmCaipChainId(CHAIN_IDS.XDC),
33+
ZKSYNC_ERA: toEvmCaipChainId(CHAIN_IDS.ZKSYNC_ERA),
34+
} as const;
35+
36+
export type NetworkId = (typeof NetworkId)[keyof typeof NetworkId];
37+
38+
/**
39+
* Display names of built-in networks as rendered in the network UI, taken from
40+
* the shared constants so the E2E tests match the product.
41+
*/
42+
export const NetworkName = {
43+
ARBITRUM: ARBITRUM_DISPLAY_NAME,
44+
AVALANCHE: AVALANCHE_DISPLAY_NAME,
45+
BITCOIN: MULTICHAIN_NETWORK_TO_NICKNAME[MultichainNetworks.BITCOIN],
46+
ETHEREUM: MAINNET_DISPLAY_NAME,
47+
LINEA: LINEA_MAINNET_DISPLAY_NAME,
48+
LOCALHOST: LOCALHOST_DISPLAY_NAME,
49+
SEPOLIA: SEPOLIA_DISPLAY_NAME,
50+
TRON: MULTICHAIN_NETWORK_TO_NICKNAME[MultichainNetworks.TRON],
51+
} as const;

‎test/e2e/page-objects/pages/networks/select-network-modal.ts‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@ import { type CaipChainId } from '@metamask/utils';
22
import { convertCaipToHexChainId } from '../../../../../shared/lib/network.utils';
33
import { Driver } from '../../../webdriver/driver';
44

5-
export enum NetworkId {
6-
ETHEREUM = 'eip155:1',
7-
LINEA = 'eip155:59144',
8-
ARBITRUM = 'eip155:42161',
9-
AVALANCHE = 'eip155:43114',
10-
BSC = 'eip155:56',
11-
BASE = 'eip155:8453',
12-
OPTIMISM = 'eip155:10',
13-
POLYGON = 'eip155:137',
14-
XDC = 'eip155:50',
15-
}
16-
175
/**
186
* The network filter keys EVM rows by hex chain id and non-EVM rows by CAIP
197
* chain id, while tests refer to networks by CAIP chain id throughout.

‎test/e2e/tests/mm-connect/connect-multichain.spec.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
SOLANA_MAINNET_SCOPE,
77
WINDOW_TITLES,
88
} from '../../constants';
9+
import { CHAIN_IDS } from '../../../../shared/constants/chain-ids';
910
import { withFixtures } from '../../helpers';
1011
import FixtureBuilderV2 from '../../fixtures/fixture-builder-v2';
1112
import { login } from '../../page-objects/flows/login.flow';
@@ -203,9 +204,9 @@ describe('MM Connect — Multichain E2E', function (this: Suite) {
203204

204205
// eth_chainId must return the hex chain ID specific to each scope
205206
const expectedChainIds: [string, string][] = [
206-
[MM_CONNECT_EVM_CHAINS.LOCALHOST, '0x539'],
207-
[MM_CONNECT_EVM_CHAINS.POLYGON, '0x89'],
208-
[MM_CONNECT_EVM_CHAINS.LINEA, '0xe708'],
207+
[MM_CONNECT_EVM_CHAINS.LOCALHOST, CHAIN_IDS.LOCALHOST],
208+
[MM_CONNECT_EVM_CHAINS.POLYGON, CHAIN_IDS.POLYGON],
209+
[MM_CONNECT_EVM_CHAINS.LINEA, CHAIN_IDS.LINEA_MAINNET],
209210
];
210211

211212
for (const [chainId, expectedHex] of expectedChainIds) {

‎test/e2e/tests/multichain/aggregated-balances.spec.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Anvil } from '../../seeder/anvil';
1616
import { switchToNetworkFromNetworkSelect } from '../../page-objects/flows/network.flow';
1717
import { CHAIN_IDS } from '../../../../shared/constants/network';
1818
import { DEFAULT_FIXTURE_ACCOUNT_ID } from '../../constants';
19+
import { NetworkName } from '../../helpers/network-constants';
1920
import {
2021
getMockAssetsPrice,
2122
mockPriceApi,
@@ -24,8 +25,8 @@ import {
2425
} from '../tokens/utils/mocks';
2526

2627
const EXPECTED_BALANCE_USD = '$85,025.00';
27-
const NETWORK_NAME_MAINNET = 'Ethereum';
28-
const NETWORK_NAME_SEPOLIA = 'Sepolia';
28+
const NETWORK_NAME_MAINNET = NetworkName.ETHEREUM;
29+
const NETWORK_NAME_SEPOLIA = NetworkName.SEPOLIA;
2930

3031
const SEPOLIA_NATIVE_ASSET_ID = 'eip155:11155111/slip44:60';
3132
const SEPOLIA_NATIVE_ZERO_ADDRESS_ASSET_ID =

‎test/e2e/tests/multichain/asset-list.spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import {
1717
DEFAULT_FIXTURE_ACCOUNT_LOWERCASE,
1818
NETWORK_CLIENT_ID,
1919
} from '../../constants';
20+
import { NetworkName } from '../../helpers/network-constants';
2021

21-
const NETWORK_NAME_MAINNET = 'Ethereum';
22+
const NETWORK_NAME_MAINNET = NetworkName.ETHEREUM;
2223
const HST_TOKEN_ADDRESS = '0x581c3C1A2A4EBDE2A0Df29B5cf4c116E42945947';
2324
const MUSD_ADDRESS = '0xacA92E438df0B2401fF60dA7E4337B687a2435DA';
2425
const MUSD_MAINNET_ASSET_ID = `eip155:1/erc20:${MUSD_ADDRESS}`;

‎test/e2e/tests/network/custom-rpc-history.spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Homepage from '../../page-objects/pages/home/homepage';
99
import NetworksPage from '../../page-objects/pages/networks/networks-page';
1010
import { login } from '../../page-objects/flows/login.flow';
1111
import HeaderNavbar from '../../page-objects/pages/home/header-navbar';
12+
import { NetworkId } from '../../helpers/network-constants';
1213

1314
describe('Custom RPC history', function (this: Suite) {
1415
it(`creates first custom RPC entry`, async function () {
@@ -189,7 +190,7 @@ describe('Custom RPC history', function (this: Suite) {
189190
await networksPage.checkNetworkOptionIsDisplayed('Localhost 8545');
190191

191192
// Only recent 3 are found and in correct order (most recent at the top)
192-
await networksPage.openNetworkRPC('eip155:1337');
193+
await networksPage.openNetworkRPC(NetworkId.LOCALHOST);
193194
await networksPage.checkNetworkRPCNumber(3);
194195
},
195196
);

‎test/e2e/tests/network/is-present-bitcoin.spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import NetworkFilter from '../../page-objects/pages/networks/network-filter';
99
import NetworksPage from '../../page-objects/pages/networks/networks-page';
1010
import SelectNetworkModal from '../../page-objects/pages/networks/select-network-modal';
1111
import { Driver } from '../../webdriver/driver';
12+
import { NetworkName } from '../../helpers/network-constants';
1213

13-
const BITCOIN_NETWORK_NAME = 'Bitcoin';
14+
const BITCOIN_NETWORK_NAME = NetworkName.BITCOIN;
1415

1516
// Anvil is still needed because the extension polls EVM networks even in
1617
// Bitcoin-only flows.

‎test/e2e/tests/network/is-present-tron.spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import HeaderNavbar from '../../page-objects/pages/home/header-navbar';
1111
import NetworksPage from '../../page-objects/pages/networks/networks-page';
1212
import { TRON_CHAIN_ID, mockTronFeatureFlags } from '../tron/mocks/common-tron';
1313
import { switchToNetworkFromNetworkSelect } from '../../page-objects/flows/network.flow';
14+
import { NetworkName } from '../../helpers/network-constants';
1415

15-
const TRON_NETWORK_NAME = 'Tron';
16+
const TRON_NETWORK_NAME = NetworkName.TRON;
1617
const TRON_NILE_NAME = 'Tron Nile';
1718
const TRON_SHASTA_NAME = 'Tron Shasta';
1819

0 commit comments

Comments
 (0)