-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.ts
More file actions
393 lines (314 loc) · 11.2 KB
/
Copy pathindex.test.ts
File metadata and controls
393 lines (314 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import { beforeEach, describe, expect, test } from "bun:test";
import { useFetchMock, type UrlOrPath } from "./index";
describe("bun-fetch-mock", () => {
const fetchMock = useFetchMock({ baseUrl: "https://api.example.com/" });
beforeEach(() => {
fetchMock.reset();
});
describe("Basic HTTP methods", () => {
test("GET request with JSON response", async () => {
const testData = { id: 1, name: "John Doe" };
fetchMock.get("https://api.example.com/users/1", {
data: testData,
});
const response = await fetch("https://api.example.com/users/1");
const data = await response.json();
expect(response.status).toBe(200);
expect(data).toEqual(testData);
fetchMock.assertAllMocksUsed();
});
test("POST request with custom status", async () => {
const newUser = { id: 2, name: "Jane Doe" };
fetchMock.post("https://api.example.com/users", {
data: newUser,
status: 201,
statusText: "Created",
});
const response = await fetch("https://api.example.com/users", {
method: "POST",
body: JSON.stringify({ name: "Jane Doe" }),
});
const data = await response.json();
expect(response.status).toBe(201);
expect(response.statusText).toBe("Created");
expect(data).toEqual(newUser);
fetchMock.assertAllMocksUsed();
});
test("PUT request", async () => {
const updatedUser = { id: 1, name: "John Smith" };
fetchMock.put("https://api.example.com/users/1", {
data: updatedUser,
});
const response = await fetch("https://api.example.com/users/1", {
method: "PUT",
body: JSON.stringify(updatedUser),
});
const data = await response.json();
expect(data).toEqual(updatedUser);
fetchMock.assertAllMocksUsed();
});
test("DELETE request", async () => {
fetchMock.delete("https://api.example.com/users/1", {
status: 204,
});
const response = await fetch("https://api.example.com/users/1", {
method: "DELETE",
});
expect(response.status).toBe(204);
fetchMock.assertAllMocksUsed();
});
test("PATCH request", async () => {
const patchData = { name: "John Updated" };
fetchMock.patch("https://api.example.com/users/1", {
data: patchData,
});
const response = await fetch("https://api.example.com/users/1", {
method: "PATCH",
body: JSON.stringify({ name: "John Updated" }),
});
const data = await response.json();
expect(data).toEqual(patchData);
fetchMock.assertAllMocksUsed();
});
test("HEAD request", async () => {
fetchMock.head("https://api.example.com/users/1", {
status: 200,
headers: { "X-User-Exists": "true" },
});
const response = await fetch("https://api.example.com/users/1", {
method: "HEAD",
});
expect(response.status).toBe(200);
expect(response.headers.get("X-User-Exists")).toBe("true");
expect(await response.text()).toBe(""); // HEAD should have no body
fetchMock.assertAllMocksUsed();
});
test("HEAD request ignores provided response data", async () => {
fetchMock.head("https://api.example.com/users/2", {
data: { ignored: true },
status: 200,
});
const response = await fetch("https://api.example.com/users/2", {
method: "HEAD",
});
expect(response.status).toBe(200);
expect(await response.text()).toBe("");
fetchMock.assertAllMocksUsed();
});
test("OPTIONS request", async () => {
fetchMock.options("https://api.example.com/users", {
status: 200,
headers: { Allow: "GET, POST, PUT, DELETE" },
});
const response = await fetch("https://api.example.com/users", {
method: "OPTIONS",
});
expect(response.status).toBe(200);
expect(response.headers.get("Allow")).toBe("GET, POST, PUT, DELETE");
fetchMock.assertAllMocksUsed();
});
});
describe("Response handling", () => {
test("String response", async () => {
fetchMock.get("https://api.example.com/health", {
data: "OK",
});
const response = await fetch("https://api.example.com/health");
const text = await response.text();
expect(response.headers.get("Content-Type")).toBe("text/plain");
expect(text).toBe("OK");
fetchMock.assertAllMocksUsed();
});
test("String response uses explicit Content-Type header when provided", async () => {
fetchMock.get("https://api.example.com/report", {
data: "id,name\n1,Ada",
headers: { "Content-Type": "text/csv" },
});
const response = await fetch("https://api.example.com/report");
const text = await response.text();
expect(response.headers.get("Content-Type")).toBe("text/csv");
expect(text).toBe("id,name\n1,Ada");
fetchMock.assertAllMocksUsed();
});
test("Empty response", async () => {
fetchMock.get("https://api.example.com/empty", {
status: 204,
});
const response = await fetch("https://api.example.com/empty");
const text = await response.text();
expect(response.status).toBe(204);
expect(text).toBe("");
fetchMock.assertAllMocksUsed();
});
test("Custom headers", async () => {
fetchMock.get("https://api.example.com/data", {
data: { message: "test" },
headers: {
"X-Custom-Header": "custom-value",
"X-Rate-Limit": "100",
},
});
const response = await fetch("https://api.example.com/data");
expect(response.headers.get("X-Custom-Header")).toBe("custom-value");
expect(response.headers.get("X-Rate-Limit")).toBe("100");
fetchMock.assertAllMocksUsed();
});
});
describe("Base URL support", () => {
test("Uses base URL for relative paths without adding double slashes", async () => {
fetchMock.get("/users", {
data: [{ id: 1, name: "John" }],
});
const response = await fetch("https://api.example.com/users");
const data = await response.json();
expect(data).toEqual([{ id: 1, name: "John" }]);
fetchMock.assertAllMocksUsed();
});
test("Works with absolute URLs even with baseUrl set", async () => {
fetchMock.get("https://other-api.com/data", {
data: { source: "other" },
});
const response = await fetch("https://other-api.com/data");
const data = await response.json();
expect(data).toEqual({ source: "other" });
fetchMock.assertAllMocksUsed();
});
});
describe("One-time mocks", () => {
test("Once mock is removed after first use", async () => {
fetchMock.get("https://api.example.com/data", {
data: { count: 1 },
once: true,
});
// First call should work
const response1 = await fetch("https://api.example.com/data");
const data1 = await response1.json();
expect(data1).toEqual({ count: 1 });
// Second call should fail
await expect(fetch("https://api.example.com/data")).rejects.toThrow(
"No mock found for [GET] https://api.example.com/data",
);
});
test("Multiple mocks with same URL", async () => {
fetchMock
.get("https://api.example.com/data", {
data: { attempt: 1 },
once: true,
})
.get("https://api.example.com/data", {
data: { attempt: 2 },
});
// First call uses the once mock
const response1 = await fetch("https://api.example.com/data");
const data1 = await response1.json();
expect(data1).toEqual({ attempt: 1 });
// Second call uses the persistent mock
const response2 = await fetch("https://api.example.com/data");
const data2 = await response2.json();
expect(data2).toEqual({ attempt: 2 });
});
});
describe("Error handling", () => {
test("Throws error for unmocked request", async () => {
await expect(fetch("https://api.example.com/unknown")).rejects.toThrow(
"No mock found for [GET] https://api.example.com/unknown",
);
});
test("Throws error for unsupported HTTP method", async () => {
await expect(
fetch("https://api.example.com/data", { method: "TRACE" }),
).rejects.toThrow("Unsupported HTTP method: TRACE");
});
test("Validates URL format", () => {
expect(() => {
fetchMock.get("invalid-url" as UrlOrPath);
}).toThrow(
"Invalid URL for GET mock: URL must start with http://, https://, or /",
);
});
test("Validates URL is non-empty", () => {
expect(() => {
fetchMock.get("" as UrlOrPath);
}).toThrow("Invalid URL for GET mock: URL must be a non-empty string");
});
test("Shows available mocks in error message", async () => {
fetchMock.get("https://api.example.com/users", { data: [] });
fetchMock.post("https://api.example.com/users", { data: {} });
await expect(fetch("https://api.example.com/unknown")).rejects.toThrow(
"No mock found for [GET] https://api.example.com/unknown. Available mocks: [GET] https://api.example.com/users, [POST] https://api.example.com/users",
);
});
});
describe("Utility methods", () => {
test("reset() clears all mocks", async () => {
fetchMock.get("https://api.example.com/data", { data: "test" });
fetchMock.reset();
await expect(fetch("https://api.example.com/data")).rejects.toThrow(
"No mock found for [GET] https://api.example.com/data",
);
});
test("assertAllMocksUsed() throws when mocks are unused", () => {
fetchMock.get("https://api.example.com/data", { data: "test" });
// Should throw since we haven't called the mock yet.
expect(() => fetchMock.assertAllMocksUsed()).toThrow();
});
test("assertAllMocksUsed() passes when all mocks are used", async () => {
fetchMock.get("https://api.example.com/data", { data: "test" });
await fetch("https://api.example.com/data");
// Now it should pass.
expect(() => fetchMock.assertAllMocksUsed()).not.toThrow();
});
});
describe("Method normalization", () => {
test("Treats lowercase method values as valid HTTP methods", async () => {
fetchMock.post("https://api.example.com/users", {
data: { id: 3, name: "Lowercase Method" },
status: 201,
});
const response = await fetch("https://api.example.com/users", {
method: "post",
});
const data = await response.json();
expect(response.status).toBe(201);
expect(data).toEqual({ id: 3, name: "Lowercase Method" });
fetchMock.assertAllMocksUsed();
});
test("Treats mixed-case method values as valid HTTP methods", async () => {
fetchMock.patch("https://api.example.com/users/1", {
data: { id: 1, name: "Mixed Case" },
});
const response = await fetch("https://api.example.com/users/1", {
method: "pAtCh",
});
const data = await response.json();
expect(response.status).toBe(200);
expect(data).toEqual({ id: 1, name: "Mixed Case" });
fetchMock.assertAllMocksUsed();
});
});
describe("Method chaining", () => {
test("Can chain multiple mock definitions", async () => {
fetchMock
.get("https://api.example.com/users", { data: [] })
.post("https://api.example.com/users", { data: {}, status: 201 })
.put("https://api.example.com/users/1", { data: {} })
.delete("https://api.example.com/users/1", { status: 204 });
// Test all the chained mocks work
const getResponse = await fetch("https://api.example.com/users");
expect(getResponse.status).toBe(200);
const postResponse = await fetch("https://api.example.com/users", {
method: "POST",
});
expect(postResponse.status).toBe(201);
const putResponse = await fetch("https://api.example.com/users/1", {
method: "PUT",
});
expect(putResponse.status).toBe(200);
const deleteResponse = await fetch("https://api.example.com/users/1", {
method: "DELETE",
});
expect(deleteResponse.status).toBe(204);
fetchMock.assertAllMocksUsed();
});
});
});