Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testfix(transaction-controller): Refactor provider setup to provide correct providers and tracker #4391

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions packages/transaction-controller/src/TransactionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,18 @@ function waitForTransactionFinished(
}

const MOCK_PREFERENCES = { state: { selectedAddress: 'foo' } };
const INFURA_PROJECT_ID = '341eacb578dd44a1a049cbc5f6fd4035';
const MAINNET_PROVIDER = new HttpProvider(
`https://mainnet.infura.io/v3/${INFURA_PROJECT_ID}`,
);
const PALM_PROVIDER = new HttpProvider(
`https://palm-mainnet.infura.io/v3/${INFURA_PROJECT_ID}`,
);
const INFURA_PROJECT_ID = 'testinfuraid';
const HTTP_PROVIDERS = {
goerli: new HttpProvider('https://goerli.infura.io/v3/goerli-pid'),
// TODO: Investigate and address why tests break when mainet has a different INFURA_PROJECT_ID
mainnet: new HttpProvider(
`https://mainnet.infura.io/v3/${INFURA_PROJECT_ID}`,
),
linea: new HttpProvider('https://linea.infura.io/v3/linea-pid'),
lineaGoerli: new HttpProvider('https://linea-g.infura.io/v3/linea-g-pid'),
custom: new HttpProvider(`http://127.0.0.123:456/ethrpc?apiKey=foobar`),
palm: new HttpProvider('https://palm-mainnet.infura.io/v3/palm-pid'),
};

type MockNetwork = {
chainId: Hex;
Expand All @@ -323,8 +328,8 @@ type MockNetwork = {

const MOCK_NETWORK: MockNetwork = {
chainId: ChainId.goerli,
provider: MAINNET_PROVIDER,
blockTracker: buildMockBlockTracker('0x102833C', MAINNET_PROVIDER),
provider: HTTP_PROVIDERS.goerli,
blockTracker: buildMockBlockTracker('0x102833C', HTTP_PROVIDERS.goerli),
Copy link
Contributor Author

@legobeat legobeat Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Mismatching provider here (e.g. supplying HTTPS_PROVIDERS.mainnet) did not cause tests to fail. This suggests that some further coverage could be desired, to catch situations were blockTracker and provider inadvertently are on different chains/networks?

state: {
selectedNetworkClientId: NetworkType.goerli,
networksMetadata: {
Expand All @@ -345,8 +350,8 @@ const MOCK_NETWORK: MockNetwork = {

const MOCK_MAINNET_NETWORK: MockNetwork = {
chainId: ChainId.mainnet,
provider: MAINNET_PROVIDER,
blockTracker: buildMockBlockTracker('0x102833C', MAINNET_PROVIDER),
provider: HTTP_PROVIDERS.mainnet,
blockTracker: buildMockBlockTracker('0x102833C', HTTP_PROVIDERS.mainnet),
state: {
selectedNetworkClientId: NetworkType.mainnet,
networksMetadata: {
Expand All @@ -367,8 +372,8 @@ const MOCK_MAINNET_NETWORK: MockNetwork = {

const MOCK_LINEA_MAINNET_NETWORK: MockNetwork = {
chainId: ChainId['linea-mainnet'],
provider: PALM_PROVIDER,
blockTracker: buildMockBlockTracker('0xA6EDFC', PALM_PROVIDER),
provider: HTTP_PROVIDERS.linea,
blockTracker: buildMockBlockTracker('0xA6EDFC', HTTP_PROVIDERS.linea),
state: {
selectedNetworkClientId: NetworkType['linea-mainnet'],
networksMetadata: {
Expand All @@ -389,8 +394,8 @@ const MOCK_LINEA_MAINNET_NETWORK: MockNetwork = {

const MOCK_LINEA_GOERLI_NETWORK: MockNetwork = {
chainId: ChainId['linea-goerli'],
provider: PALM_PROVIDER,
blockTracker: buildMockBlockTracker('0xA6EDFC', PALM_PROVIDER),
provider: HTTP_PROVIDERS.lineaGoerli,
blockTracker: buildMockBlockTracker('0xA6EDFC', HTTP_PROVIDERS.lineaGoerli),
state: {
selectedNetworkClientId: NetworkType['linea-goerli'],
networksMetadata: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ function newMultichainTrackingHelper(
describe('MultichainTrackingHelper', () => {
beforeEach(() => {
jest.resetAllMocks();

for (const network of [
'mainnet',
'goerli',
'sepolia',
'customNetworkClientId-1',
] as const) {
MOCK_BLOCK_TRACKERS[network] = buildMockBlockTracker(network);
Copy link
Contributor

@mcmire mcmire Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem this solves is a good example of why sharing variables among tests by initializing them outside of all tests can lead to misleading and difficult to debug behavior (since a variable may have state that's changed in one test and then bleeds over into another test). This fix makes sense, but if we really need specific block trackers and providers it would be better to create them on the fly in tests, using a helper function to assist. See here for more: https://github.com/MetaMask/contributor-docs/blob/main/docs/unit-testing.md#reset-shared-variables (expand the triangle for an example)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fully agreed! The end state here is still not as I'd like it either but had to draw the line somewhere ;^^

MOCK_PROVIDERS[network] = buildMockProvider(network);
}
});

describe('onNetworkStateChange', () => {
Expand Down
Loading