Commit 3a1777b
authored
Replace @fetch-mock/jest with plain fetch-mock (#5575)
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. 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:
```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.File tree
8 files changed
+107
-56
lines changed- src
- test
- components
- store
- unit
- types/globals
8 files changed
+107
-56
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
118 | | - | |
119 | 118 | | |
120 | 119 | | |
121 | 120 | | |
| |||
158 | 157 | | |
159 | 158 | | |
160 | 159 | | |
| 160 | + | |
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
210 | | - | |
| 210 | + | |
211 | 211 | | |
212 | 212 | | |
213 | 213 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
175 | 181 | | |
176 | 182 | | |
177 | 183 | | |
| |||
193 | 199 | | |
194 | 200 | | |
195 | 201 | | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
207 | 219 | | |
208 | 220 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
| 51 | + | |
| 52 | + | |
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
813 | 813 | | |
814 | 814 | | |
815 | 815 | | |
816 | | - | |
817 | | - | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
818 | 821 | | |
819 | 822 | | |
820 | 823 | | |
| |||
828 | 831 | | |
829 | 832 | | |
830 | 833 | | |
831 | | - | |
832 | | - | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
833 | 839 | | |
834 | 840 | | |
835 | 841 | | |
| |||
917 | 923 | | |
918 | 924 | | |
919 | 925 | | |
920 | | - | |
921 | | - | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
922 | 931 | | |
923 | 932 | | |
924 | 933 | | |
| |||
1414 | 1423 | | |
1415 | 1424 | | |
1416 | 1425 | | |
1417 | | - | |
1418 | | - | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
1419 | 1431 | | |
1420 | 1432 | | |
1421 | 1433 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
228 | 228 | | |
229 | 229 | | |
230 | 230 | | |
231 | | - | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
232 | 234 | | |
233 | 235 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
69 | | - | |
70 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
71 | 73 | | |
72 | 74 | | |
73 | 75 | | |
| |||
84 | 86 | | |
85 | 87 | | |
86 | 88 | | |
87 | | - | |
88 | | - | |
89 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
90 | 94 | | |
91 | 95 | | |
92 | 96 | | |
| |||
110 | 114 | | |
111 | 115 | | |
112 | 116 | | |
113 | | - | |
114 | | - | |
115 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
116 | 122 | | |
117 | 123 | | |
118 | 124 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1279 | 1279 | | |
1280 | 1280 | | |
1281 | 1281 | | |
1282 | | - | |
1283 | | - | |
1284 | | - | |
1285 | | - | |
1286 | | - | |
1287 | | - | |
1288 | | - | |
1289 | 1282 | | |
1290 | 1283 | | |
1291 | 1284 | | |
| |||
11324 | 11317 | | |
11325 | 11318 | | |
11326 | 11319 | | |
11327 | | - | |
| 11320 | + | |
| 11321 | + | |
| 11322 | + | |
| 11323 | + | |
| 11324 | + | |
| 11325 | + | |
| 11326 | + | |
| 11327 | + | |
| 11328 | + | |
| 11329 | + | |
11328 | 11330 | | |
11329 | 11331 | | |
11330 | 11332 | | |
| |||
11446 | 11448 | | |
11447 | 11449 | | |
11448 | 11450 | | |
11449 | | - | |
| 11451 | + | |
11450 | 11452 | | |
11451 | 11453 | | |
11452 | 11454 | | |
| |||
11460 | 11462 | | |
11461 | 11463 | | |
11462 | 11464 | | |
| 11465 | + | |
| 11466 | + | |
| 11467 | + | |
| 11468 | + | |
| 11469 | + | |
| 11470 | + | |
| 11471 | + | |
11463 | 11472 | | |
11464 | 11473 | | |
11465 | 11474 | | |
| |||
12975 | 12984 | | |
12976 | 12985 | | |
12977 | 12986 | | |
12978 | | - | |
| 12987 | + | |
| 12988 | + | |
| 12989 | + | |
| 12990 | + | |
| 12991 | + | |
| 12992 | + | |
| 12993 | + | |
| 12994 | + | |
| 12995 | + | |
| 12996 | + | |
12979 | 12997 | | |
12980 | 12998 | | |
12981 | 12999 | | |
| |||
0 commit comments