Skip to content

Fix nock compatibility with fake timers #4428

Open
@Gudahtt

Description

@Gudahtt

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 sinonjs/fake-timers#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: MetaMask/metamask-extension#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.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions