(config: MockFetchResponse = {})
| 37 | * Creates a mock Response object. |
| 38 | */ |
| 39 | export function createMockResponse(config: MockFetchResponse = {}): Response { |
| 40 | const status = config.status ?? 200 |
| 41 | const ok = config.ok ?? (status >= 200 && status < 300) |
| 42 | |
| 43 | return { |
| 44 | status, |
| 45 | statusText: config.statusText ?? (ok ? 'OK' : 'Error'), |
| 46 | ok, |
| 47 | headers: new Headers(config.headers ?? {}), |
| 48 | json: vi.fn(async () => config.json ?? {}), |
| 49 | text: vi.fn(async () => config.text ?? JSON.stringify(config.json ?? {})), |
| 50 | body: config.body ?? null, |
| 51 | bodyUsed: false, |
| 52 | arrayBuffer: vi.fn(async () => new ArrayBuffer(0)), |
| 53 | blob: vi.fn(async () => new Blob()), |
| 54 | formData: vi.fn(async () => new FormData()), |
| 55 | clone: vi.fn(function (this: Response) { |
| 56 | return createMockResponse(config) |
| 57 | }), |
| 58 | redirected: false, |
| 59 | type: 'basic' as ResponseType, |
| 60 | url: '', |
| 61 | bytes: vi.fn(async () => new Uint8Array()), |
| 62 | } as Response |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Creates a mock fetch that handles multiple URLs with different responses. |
no outgoing calls
no test coverage detected