Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest simulate offline network with Mock Service Worker (MSW) or other?

Tags:

jestjs

msw

Is it possible to use MSW with Jest to simulate offline network conditions? Alternatively non MSW based solutions would suffice. I also don't want to mock navigator.onLine as I want to test the error path when a network request fails due to being offline.

like image 712
rssfrncs Avatar asked Nov 01 '25 16:11

rssfrncs


1 Answers

Yes you can simulate a network error:

rest.get('/user', (_, res) => {
  return res.networkError('Custom network error message')
})

Here is the related documentation: https://mswjs.io/docs/api/response/network-error

like image 141
abumalick Avatar answered Nov 04 '25 20:11

abumalick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!