Skip to content

Commit d750042

Browse files
chore(deps): update dependency fetch-mock to v12 - abandoned (#316)
* chore(deps): update dependency fetch-mock to v12 * updates tests to use the new v12 fetch-mock apis --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Nick Floyd <[email protected]>
1 parent fa14d5c commit d750042

File tree

4 files changed

+148
-81
lines changed

4 files changed

+148
-81
lines changed

package-lock.json

Lines changed: 39 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"@octokit/tsconfig": "^4.0.0",
3434
"@vitest/coverage-v8": "^2.0.3",
3535
"esbuild": "^0.24.0",
36-
"fetch-mock": "^11.0.0",
36+
"fetch-mock": "^12.0.0",
3737
"glob": "^11.0.0",
3838
"prettier": "3.4.2",
3939
"typescript": "^5.0.0",

test/index.test.ts

Lines changed: 100 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,32 @@ test('auth.hook(request, "GET /repos/octocat/hello-world")', async () => {
3636
"user-agent": "test",
3737
};
3838

39-
const matchGetUser: fetchMock.MockMatcherFunction = (url, { headers }) => {
40-
expect(url).toEqual("https://api.github.com/repos/octocat/hello-world");
41-
expect(headers).toStrictEqual(expectedRequestHeaders);
39+
const matchGetUser: fetchMock.MockMatcherFunction = (request) => {
40+
expect(request.url).toEqual(
41+
"https://api.github.com/repos/octocat/hello-world",
42+
);
43+
44+
const headersObject = mock.callHistory.calls()[0].options.headers!;
45+
expect(headersObject).toStrictEqual(expectedRequestHeaders);
46+
4247
return true;
4348
};
4449

50+
const mock = fetchMock.createInstance();
51+
52+
mock.getOnce(matchGetUser, {
53+
status: 200,
54+
body: {
55+
id: 123,
56+
},
57+
});
58+
4559
const requestMock = request.defaults({
4660
headers: {
4761
"user-agent": "test",
4862
},
4963
request: {
50-
fetch: fetchMock.sandbox().getOnce(matchGetUser, { id: 123 }),
64+
fetch: mock.fetchHandler,
5165
},
5266
});
5367

@@ -63,29 +77,38 @@ test('auth.hook(request, "GET /repos/octocat/hello-world") returns 404', async (
6377
"user-agent": "test",
6478
};
6579

66-
const matchGetUser: fetchMock.MockMatcherFunction = (url, { headers }) => {
67-
expect(url).toEqual("https://api.github.com/repos/octocat/hello-world");
68-
expect(headers).toStrictEqual(expectedRequestHeaders);
80+
const matchGetUser: fetchMock.MockMatcherFunction = (request) => {
81+
expect(request.url).toEqual(
82+
"https://api.github.com/repos/octocat/hello-world",
83+
);
84+
85+
const headersObject = mock.callHistory.calls()[0].options.headers!;
86+
expect(headersObject).toStrictEqual(expectedRequestHeaders);
87+
6988
return true;
7089
};
7190

91+
const mock = fetchMock.createInstance();
92+
mock.getOnce(matchGetUser, {
93+
status: 404,
94+
body: {
95+
message: "Not Found",
96+
},
97+
});
98+
7299
const requestMock = request.defaults({
73100
headers: {
74101
"user-agent": "test",
75102
},
76103
request: {
77-
fetch: fetchMock.sandbox().getOnce(matchGetUser, {
78-
status: 404,
79-
body: {
80-
message: "Not Found",
81-
},
82-
}),
104+
fetch: mock.fetchHandler,
83105
},
84106
});
85107

86108
const { hook } = createUnauthenticatedAuth({ reason: "test" });
87109

88110
try {
111+
// Call the hook with the requestMock
89112
await hook(requestMock, "GET /repos/octocat/hello-world");
90113
throw new Error("should not resolve");
91114
} catch (error: any) {
@@ -101,27 +124,34 @@ test('auth.hook(request, "GET /repos/octocat/hello-world") returns rate limit re
101124
"user-agent": "test",
102125
};
103126

104-
const matchGetUser: fetchMock.MockMatcherFunction = (url, { headers }) => {
105-
expect(url).toEqual("https://api.github.com/repos/octocat/hello-world");
106-
expect(headers).toStrictEqual(expectedRequestHeaders);
127+
const matchGetUser: fetchMock.MockMatcherFunction = (request) => {
128+
expect(request.url).toEqual(
129+
"https://api.github.com/repos/octocat/hello-world",
130+
);
131+
132+
const headersObject = mock.callHistory.calls()[0].options.headers!;
133+
expect(headersObject).toStrictEqual(expectedRequestHeaders);
134+
107135
return true;
108136
};
109137

138+
const mock = fetchMock.createInstance().getOnce(matchGetUser, {
139+
status: 403,
140+
headers: {
141+
"x-ratelimit-remaining": 0,
142+
},
143+
body: {
144+
message:
145+
"API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
146+
},
147+
});
148+
110149
const requestMock = request.defaults({
111150
headers: {
112151
"user-agent": "test",
113152
},
114153
request: {
115-
fetch: fetchMock.sandbox().getOnce(matchGetUser, {
116-
status: 403,
117-
headers: {
118-
"x-ratelimit-remaining": 0,
119-
},
120-
body: {
121-
message:
122-
"API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
123-
},
124-
}),
154+
fetch: mock.fetchHandler,
125155
},
126156
});
127157

@@ -143,24 +173,31 @@ test('auth.hook(request, "GET /repos/octocat/hello-world") returns rate limit re
143173
"user-agent": "test",
144174
};
145175

146-
const matchGetUser: fetchMock.MockMatcherFunction = (url, { headers }) => {
147-
expect(url).toEqual("https://api.github.com/repos/octocat/hello-world");
148-
expect(headers).toStrictEqual(expectedRequestHeaders);
176+
const matchGetUser: fetchMock.MockMatcherFunction = (request) => {
177+
expect(request.url).toEqual(
178+
"https://api.github.com/repos/octocat/hello-world",
179+
);
180+
181+
const headersObject = mock.callHistory.calls()[0].options.headers!;
182+
expect(headersObject).toStrictEqual(expectedRequestHeaders);
183+
149184
return true;
150185
};
151186

187+
const mock = fetchMock.createInstance().getOnce(matchGetUser, {
188+
status: 403,
189+
body: {
190+
message:
191+
"You have triggered an abuse detection mechanism and have been temporarily blocked from content creation. Please retry your request again later.",
192+
},
193+
});
194+
152195
const requestMock = request.defaults({
153196
headers: {
154197
"user-agent": "test",
155198
},
156199
request: {
157-
fetch: fetchMock.sandbox().getOnce(matchGetUser, {
158-
status: 403,
159-
body: {
160-
message:
161-
"You have triggered an abuse detection mechanism and have been temporarily blocked from content creation. Please retry your request again later.",
162-
},
163-
}),
200+
fetch: mock.fetchHandler,
164201
},
165202
});
166203

@@ -177,14 +214,15 @@ test('auth.hook(request, "GET /repos/octocat/hello-world") returns rate limit re
177214
});
178215

179216
test('auth.hook(request, "PATCH /repos/octocat/hello-world") with 401 response', async () => {
217+
const mock = fetchMock
218+
.createInstance()
219+
.patchOnce("path:/repos/octocat/hello-world", 401);
180220
const requestMock = request.defaults({
181221
headers: {
182222
"user-agent": "test",
183223
},
184224
request: {
185-
fetch: fetchMock
186-
.sandbox()
187-
.patchOnce("path:/repos/octocat/hello-world", 401),
225+
fetch: mock.fetchHandler,
188226
},
189227
});
190228

@@ -201,14 +239,21 @@ test('auth.hook(request, "PATCH /repos/octocat/hello-world") with 401 response',
201239
});
202240

203241
test('auth.hook(request, "GET /repos/octocat/hello-world") does not swallow non-request related errors', async () => {
242+
const mock = fetchMock
243+
.createInstance()
244+
.get("path:/repos/octocat/hello-world", {
245+
status: 500,
246+
body: {
247+
message: "unrelated",
248+
},
249+
});
250+
204251
const requestMock = request.defaults({
205252
headers: {
206253
"user-agent": "test",
207254
},
208255
request: {
209-
fetch() {
210-
throw new Error("unrelated");
211-
},
256+
fetch: mock.fetchHandler,
212257
},
213258
});
214259

@@ -223,19 +268,21 @@ test('auth.hook(request, "GET /repos/octocat/hello-world") does not swallow non-
223268
});
224269

225270
test('auth.hook(request, "POST /repos/octocat/hello-world/issues/123/comments") with 403 response', async () => {
271+
const mock = fetchMock
272+
.createInstance()
273+
.postOnce("path:/repos/octocat/hello-world/issues/123/comments", {
274+
status: 403,
275+
body: {
276+
message: "You cannot comment on locked issues",
277+
},
278+
});
279+
226280
const requestMock = request.defaults({
227281
headers: {
228282
"user-agent": "test",
229283
},
230284
request: {
231-
fetch: fetchMock
232-
.sandbox()
233-
.postOnce("path:/repos/octocat/hello-world/issues/123/comments", {
234-
status: 403,
235-
body: {
236-
message: "You cannot comment on locked issues",
237-
},
238-
}),
285+
fetch: mock.fetchHandler,
239286
},
240287
});
241288

@@ -256,12 +303,14 @@ test('auth.hook(request, "POST /repos/octocat/hello-world/issues/123/comments")
256303
});
257304

258305
test("500 response", async () => {
306+
const mock = fetchMock.createInstance().getOnce("path:/", 500);
307+
259308
const requestMock = request.defaults({
260309
headers: {
261310
"user-agent": "test",
262311
},
263312
request: {
264-
fetch: fetchMock.sandbox().getOnce("path:/", 500),
313+
fetch: mock.fetchHandler,
265314
},
266315
});
267316

0 commit comments

Comments
 (0)