MetaMask / MetaMask/core

Fix `nock` compatibility with fake timers

Open
#4,428 1 comment 1 reaction 0 assignees View on GitHub
bug team-wallet-framework wf-bugs
Dominant language
TypeScript
Stars
413
Forks
308
Avg merge
1d 4h
Merged PRs (30d)
253

Description

The `nock` library is not compatible with the fake timers we use in unit tests because it uses the Node.js `timers` API. This API is not mocked correctly by the version of Jest we are using.

Jest uses `@sinon/fake-timers` internally, which didn't support mocking the Node.js `timers` API until v11.0.0 (see https://github.com/sinonjs/fake-timers/pull/467) This package is updated in Jest as part of the v30 release, which is currently under development.

Elsewhere we have worked around this problem by patching the `nock` library to use global timers rather than the `timers` API. Global timers are mocked correctly by our fake timers. We should implement the same fix here, so that fake timers work more predictably. We can remove this workaround when we update to Jest v30.

See here for how we did this in the extension repository: https://github.com/MetaMask/metamask-extension/pull/24805/

I suspect that we haven't seen this issue come up in core yet because all of our nock delays use very small intervals, so we don't notice that they aren't correctly mocked.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by locating the core unit tests and the nock dependency that use delayed requests, then compare the workaround described in the linked MetaMask extension pull request. Implement the equivalent timer compatibility change so fake timers control nock delays predictably; verify the affected tests and keep in mind that the workaround can be removed after Jest v30.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.