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

test: Fix nock compatibility with fake timers #24805

Merged
merged 1 commit into from
May 28, 2024

Conversation

Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented May 27, 2024

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 sinonjs/fake-timers#467) This package is updated in Jest as part of the v30 release, which is currently under development.

To workaround this problem in the meantime, the nock package has been updated and patched to use global timers rather than the timers API. Global timers are mocked correctly.

Open in GitHub Codespaces

Related issues

This was required for some unit tests that I will be submitting in a different PR, intended for #24503

Manual testing steps

N/A

Screenshots/Recordings

N/A

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • 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 format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). 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.

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.

To workaround this problem in the meantime, the `nock` package has been
updated and patched to use global timers rather than the `timers` API.
Global timers are mocked correctly.

This was required for some unit tests that I will be submitting in a
different PR, intended for #24503
@Gudahtt Gudahtt marked this pull request as ready for review May 27, 2024 22:18
@Gudahtt Gudahtt requested a review from a team as a code owner May 27, 2024 22:18
@metamaskbot
Copy link
Collaborator

Builds ready [c9d9ef4]
Page Load Metrics (1699 ± 917 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint6174118617082
domContentLoaded8211354722
load51507416991909917
domInteractive8211354722
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@Gudahtt Gudahtt merged commit 9971cb5 into develop May 28, 2024
76 of 77 checks passed
@Gudahtt Gudahtt deleted the fix-nock-fake-timers-compatibility branch May 28, 2024 12:33
@github-actions github-actions bot locked and limited conversation to collaborators May 28, 2024
@gauthierpetetin gauthierpetetin added the release-12.0.0 Issue or pull request that will be included in release 12.0.0 label Jun 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
release-12.0.0 Issue or pull request that will be included in release 12.0.0 team-extension-platform
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants