Replace @fetch-mock/jest with plain fetch-mock#5575
Merged
mstange merged 2 commits intofirefox-devtools:mainfrom Sep 3, 2025
Merged
Replace @fetch-mock/jest with plain fetch-mock#5575mstange merged 2 commits intofirefox-devtools:mainfrom
mstange merged 2 commits intofirefox-devtools:mainfrom
Conversation
ce76714 to
07d9ab4
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
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.
07d9ab4 to
0f4bf61
Compare
canova
approved these changes
Sep 3, 2025
Member
canova
left a comment
There was a problem hiding this comment.
Looks good to me, thanks for providing the context in the PR message!
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This makes the tests check what they were thinking they were checking, and gives better error messages when they fail.
Most of our
toHaveFetchedchecks weren't actually checking anything.Only the checks in shorten-url.test.ts worked. These were of the following shape:
So they were passing a plain object literal (not an
expect.objectContaining), and they were using an actual object in thebodyproperty, 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:
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:
If we wanted to keep using toHaveFetched here, one option would be to use fetch-mock's "body" matcher, and do something like this:
or in reality probably something more like this:
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/jestis 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.