Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit 2a5fe11

Browse files
committed
fix toHaveLastFetched and toHaveNthFetched
Was relying on equality of refernce for different calls, but now fetch-mock builds a new cll object each time, so references are different and need to do a manual comparison
1 parent f6b1024 commit 2a5fe11

File tree

3 files changed

+48
-86
lines changed

3 files changed

+48
-86
lines changed

jest-extensions.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
const callsAreEqual = (c1, c2) => {
2+
if (!c1 && !c2) return true;
3+
if (!c1 || !c2) return false;
4+
if (c1[0] !== c2[0]) return false;
5+
if (c1[1] !== c2[1]) return false;
6+
if (c1.request !== c2.request) return false;
7+
if (c1.identifier !== c2.identifier) return false;
8+
if (c1.isUnmatched !== c2.isUnmatched) return false;
9+
if (c1.response !== c2.response) return false;
10+
return true;
11+
};
12+
113
const methodlessExtensions = {
214
toHaveFetched: (fetchMock, url, options) => {
315
if (fetchMock.called(url, options)) {
@@ -18,7 +30,7 @@ const methodlessExtensions = {
1830
}
1931
const lastCall = [...allCalls].pop();
2032
const lastUrlCall = [...fetchMock.calls(url, options)].pop();
21-
if (lastCall === lastUrlCall) {
33+
if (callsAreEqual(lastCall, lastUrlCall)) {
2234
return { pass: true };
2335
}
2436
return {
@@ -31,7 +43,7 @@ const methodlessExtensions = {
3143
toHaveNthFetched: (fetchMock, n, url, options) => {
3244
const nthCall = fetchMock.calls()[n - 1];
3345
const urlCalls = fetchMock.calls(url, options);
34-
if (urlCalls.includes(nthCall)) {
46+
if (urlCalls.some((call) => callsAreEqual(call, nthCall))) {
3547
return { pass: true };
3648
}
3749
return {

0 commit comments

Comments
 (0)