MetaMask / MetaMask/core

Establish test helper function pattern across all controllers

Open
#922 0 comments 0 reactions 0 assignees View on GitHub
team-wallet-framework wf-test-quality-improvement
Dominant language
TypeScript
Stars
413
Forks
308
Avg merge
1d 4h
Merged PRs (30d)
253

Description

One pattern I've been following in other projects recently to handle steps that you'd normally put into an `afterEach` is to have your setup function take a function. I like to rename the function `withXXXX` to indicate that it doesn't return the controllers (or test data) itself. For instance you could have:

```
function withControllers(fn): Controllers {
// ...instantiate controllers...

try {
fn({ messenger, network, preferences, assetsContract });
} finally {
messenger.clearEventSubscriptions('NetworkController:stateChange');
}
}
```

The reason `withControllers` takes a function is because that would now represent the test. Here's how you'd update an existing test for instance:

```
it('should update the ipfsGateWay config value when this value is changed in the preferences controller', () => {
withControllers(({ assetsContract, messenger, preferences }) => {
expect(assetsContract.config).toStrictEqual({
chainId: SupportedTokenDetectionNetworks.mainnet,
ipfsGateway: IPFS_DEFAULT_GATEWAY_URL,
provider: undefined,
});
preferences.setIpfsGateway('newIPFSGateWay');
expect(assetsContract.config).toStrictEqual({
ipfsGateway: 'newIPFSGateWay',
chainId: SupportedTokenDetectionNetworks.mainnet,
provider: undefined,
});
});
});
```

I realize you've spent a lot of time on these tests already, so this isn't a request for this PR, but could give you ideas for a future PR.

_Originally posted by @mcmire in https://github.com/MetaMask/controllers/pull/903#discussion_r980519671_

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing the existing controller test setup functions and the tests that use them; the issue does not identify specific files or entry points. Compare their cleanup patterns with the proposed withControllers callback shape. Done means the agreed helper pattern is applied consistently across all controllers and the tests still pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing-qa
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.