Skip to content

Replace @fetch-mock/jest with plain fetch-mock#5575

Merged
mstange merged 2 commits intofirefox-devtools:mainfrom
mstange:fix-fetch-mock-checks
Sep 3, 2025
Merged

Replace @fetch-mock/jest with plain fetch-mock#5575
mstange merged 2 commits intofirefox-devtools:mainfrom
mstange:fix-fetch-mock-checks

Conversation

@mstange
Copy link
Contributor

@mstange mstange commented Aug 27, 2025

This makes the tests check what they were thinking they were checking, and gives better error messages when they fail.


Most of our toHaveFetched checks weren't actually checking anything.

Only the checks in shorten-url.test.ts worked. These were of the following shape:

expect(window.fetch).toHaveFetched({ body: { longUrl: expectedLongUrl } });

So they were passing a plain object literal (not an expect.objectContaining), and they were using an actual object in the body property, which fetch-mock compares against the JSON.parse'd body. This is the right way to use fetch-mock's "body matcher" and it worked.

The check in profile-store.test.ts also worked, but only by accident:

expect(window.fetch).toHaveFetched(endpointUrl, expect.anything());

We could have just removed the second argument there.

The uses in receive-profile.test.ts were not checking anything, and neither were the uses in AppLocalizationProvider.test.tsx.

Here's the use of toHaveFetched in in receive-profile.test.ts:

expect(window.fetch).toHaveFetched(
  'https://symbolication.services.mozilla.com/symbolicate/v5',
  expect.objectContaining({
    body: expect.stringMatching(/memoryMap.*firefox/),
  })
);

If we wanted to keep using toHaveFetched here, one option would be to use fetch-mock's "body" matcher, and do something like this:

expect(window.fetch).toHaveFetched(
  'https://symbolication.services.mozilla.com/symbolicate/v5',
  {
    body: { job: [...] },
    matchPartialBody: true,
  }
);

or in reality probably something more like this:

expect(window.fetch).toHaveFetched(
  'https://symbolication.services.mozilla.com/symbolicate/v5',
  {
    matcherFunction: (callLog) => {
      return (callLog.options.body as string).match(/memoryMap.*firefox/) !== null
    }
  }
);

But then the other problem is that, if these checks fail, the error messages are not all that useful. For example, if I misspell 'firefox' as 'forefix' in the example above, the error message I get is:

fetch should have been called with https://symbolication.services.mozilla.com/symbolicate/v5 and {}

Overall, my conclusion is that, if I can't use things like expect.objectContaining or expect.stringMatching with @fetch-mock/jest, then @fetch-mock/jest is not very useful and we're better off using plain fetch-mock.

Accessing lastCall() directly lets us use these Jest matchers and produces more useful errors.

@mstange mstange self-assigned this Aug 27, 2025
@mstange mstange requested a review from canova as a code owner August 27, 2025 20:15
@mstange mstange force-pushed the fix-fetch-mock-checks branch from ce76714 to 07d9ab4 Compare August 27, 2025 20:19
@codecov
Copy link

codecov bot commented Aug 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.78%. Comparing base (d956b46) to head (0f4bf61).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5575   +/-   ##
=======================================
  Coverage   85.78%   85.78%           
=======================================
  Files         309      309           
  Lines       30403    30403           
  Branches     8370     8370           
=======================================
  Hits        26080    26080           
  Misses       3902     3902           
  Partials      421      421           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This makes the tests check what they were thinking they were checking,
and gives better error messages when they fail.

---

Most of our `toHaveFetched` checks weren't actually checking anything.

Only the checks in shorten-url.test.ts worked. These were of the following shape:

```ts
expect(window.fetch).toHaveFetched({ body: { longUrl: expectedLongUrl } });
```

So they were passing a plain object literal (not an `expect.objectContaining`),
and they were using an actual object in the `body` property, which fetch-mock
compares against the JSON.parse'd body.

The check in profile-store.test.ts also worked, but only by accident:

```ts
expect(window.fetch).toHaveFetched(endpointUrl, expect.anything());
```

We could have just removed the second argument there.

The uses in receive-profile.test.ts were not checking anything, and neither
were the uses in AppLocalizationProvider.test.tsx.

Here's the use of toHaveFetched in in receive-profile.test.ts:

```ts
expect(window.fetch).toHaveFetched(
  'https://symbolication.services.mozilla.com/symbolicate/v5',
  expect.objectContaining({
    body: expect.stringMatching(/memoryMap.*firefox/),
  })
);
```

If we wanted to keep using toHaveFetched here, one option would  be to
use fetch-mock's "body" matcher, and do something like this:

```ts
expect(window.fetch).toHaveFetched(
  'https://symbolication.services.mozilla.com/symbolicate/v5',
  {
    body: { job: [...] },
    matchPartialBody: true,
  }
);
```

or in reality probably something more like this:

```ts
expect(window.fetch).toHaveFetched(
  'https://symbolication.services.mozilla.com/symbolicate/v5',
  {
    matcherFunction: (callLog) => {
      return (callLog.options.body as string).match(/memoryMap.*firefox/) !== null
    }
  }
);
```

But then the other problem is that, if these checks fail, the error messages
are not all that useful. For example, if I misspell 'firefox' as 'forefix' in
the example above, the error message I get is:

`fetch should have been called with https://symbolication.services.mozilla.com/symbolicate/v5 and {}`

Overall, my conclusion is that, if I can't use things like expect.objectContaining
or expect.stringMatching with `@fetch-mock/jest`, then `@fetch-mock/jest` is not very
useful and we're better off using plain fetch-mock.

Accessing `lastCall()` directly lets us use these Jest matchers and produces more useful errors.
@mstange mstange force-pushed the fix-fetch-mock-checks branch from 07d9ab4 to 0f4bf61 Compare September 3, 2025 18:41
Copy link
Member

@canova canova left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks for providing the context in the PR message!

@mstange mstange enabled auto-merge September 3, 2025 23:04
@mstange mstange merged commit 3a1777b into firefox-devtools:main Sep 3, 2025
12 of 13 checks passed
@canova canova mentioned this pull request Sep 5, 2025
canova added a commit that referenced this pull request Sep 5, 2025
Changes:

[Markus Stange] Some path fixes (#5581)
[depfu[bot]] Update all Yarn dependencies (2025-09-03) (#5585)
[Nazım Can Altınova] Remove the findDOMNode usage from ContextMenu
(#5588)
[Markus Stange] Replace @fetch-mock/jest with plain fetch-mock (#5575)
[Ryan Hunt] Replace zee-worker.js with compression streams API (#5584)
[Markus Stange] Remove unused FilterNavigatorBar animations (#5591)
[Markus Stange] Use different ports for start-prod and start-examples
(#5592)
[Markus Stange] Remove SharedArrayBuffer workaround (#5596)
[Jeff Muizelaar] Make time after responseEnd have a more accurate name
(#5582)
[Ryan Hunt] Move compression and decompression to a worker (#5597)
[Markus Stange] Remove Flow and no-TypeScript settings from
.vscode/settings.json (#5601)
[Markus Stange] Simplify the eslint command in package.json so that
eslint.config.mjs is the only place you need to look if you want to know
if a file is being checked (#5600)
[Markus Stange] Fix search filtering (#5599)
[Nazım Can Altınova] 🔃 Sync: l10n -> main (September 5, 2025) (#5603)

And thanks to our localizers:

de: mstange
tr: Fernando Javier
tr: Rua
tr: Selim Şumlu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants