fix(tests): address edge cases in mock infrastructure

- Prevent spy leak on double mockFetch() call
- Differentiate body types (URLSearchParams, ArrayBuffer, Blob, ReadableStream)
- Fix route path parsing to preserve spaces
- Escape regex special chars before wildcard replacement
- Remove unused MockResponse.error and MockRoute interface
This commit is contained in:
kaitranntt committed 2026-01-23 23:41:49 -05:00
1 parent 5c83429a79
commit 2b91c40e37
3 files changed
+24 -16

No files matched your search

+8 -2
View File
@@ -55,12 +55,18 @@ export function createMockHttpServer(config: MockHttpServerConfig): MockHttpServ
// Try pattern matching for routes with wildcards
for (const [key, response] of Object.entries(routes)) {
const [routeMethod, routePath] = key.split(' ', 2);
// Split on first space only to preserve spaces in path
const spaceIndex = key.indexOf(' ');
if (spaceIndex === -1) continue;
const routeMethod = key.slice(0, spaceIndex);
const routePath = key.slice(spaceIndex + 1);
if (routeMethod !== method) continue;
// Check if route path is a pattern (contains *)
if (routePath.includes('*')) {
const pattern = routePath.replace(/\*/g, '.*');
// Escape regex special chars, then replace * with .*
const escaped = routePath.replace(/[.+?^${}()|[\]\\]/g, '\\$&');
const pattern = escaped.replace(/\*/g, '.*');
const regex = new RegExp(`^${pattern}$`);
if (regex.test(path)) {
return response;