Can I test fetch timeout using Jest?

  • node: 20.15.1
  • jest: 29.7.0
  • jest-environment-jsdom: 29.7.0
const response = await fetch(url, { signal: AbortSignal.timeout(5000) });

I want to unit test the above line of javascript using Jest. Specifically the timeout behaviour. But is that actually possible?

No matter what I do, I have to mock the fetch function. I can make it reject or timeout or whatever, but I’m really just testing the functionality of the mock.

I kinda feel like this is where JS hands over to the web API. Therefore I can’t unit test it?