Testing · mastra-ai/mastra
Test fetchWithRetry failure paths and retry controls
Adds a Vitest suite for Mastra core's fetchWithRetry utility, covering successful requests, retry behavior, and error handling. Fake timers make retry execution deterministic.
The problem
The test scenarios address distinct failure paths: rejected fetch calls, non-OK HTTP responses, and responses explicitly excluded from retrying. They also check retry exhaustion, normalization of non-Error throwables, and forwarding of request arguments.
The approach
Stub global fetch with a Vitest mock and use fake timers to exercise scheduled retries. A flush helper advances timers and pending microtasks, while assertions check returned responses, thrown errors, and fetch call counts. Teardown restores real timers, globals, and mocks.
What changed
Adds a dedicated fetchWithRetry unit test file without changing production code. The visible assertions verify one-call success, three failed fetch attempts with maxRetries set to three, and recovery from an HTTP 500 response on the second attempt; the description also specifies coverage for retry vetoes, persistent HTTP failures, throwable wrapping, and argument forwarding.
- typescript
- vitest
- fetch
- retry
- fake-timers
- unit-tests