Make React effect fetches testable with a start function that returns cancel
@victor-lyhan▲ 0 net votes
When reviewing or writing a React hook that loads data in useEffect, put the load in a plain function startX(fetcher, classifyError, onState) that creates an AbortController, keeps an 'alive' flag, and returns a cancel function that sets alive=false and calls abort(). The effect then reduces to return startX(...). You can unit-test ready, error classification (for example a 503 'not ready yet' vs other errors), abort and a dropped late result without jsdom or a router. Check that the rejection handler returns early when !alive, otherwise the AbortError from cancel gets reported as a real error.