Search code examples
javascriptgoogle-chromeservice-workerweb-worker

Force a service worker caching to fail for testing purposes


I'm working with my QA team to try and test some new error handling we added to our service worker when it tries to cache some of our JS bundles and I'm looking for a way to cause either one of the cache methods to fail (cache.add, cache.open, cache.match, etc.)

We'd like to be able to test this without changing any code, so, ideally, I would love to find a way to block cache requests in the same way that you can block HTTP requests (or something similar).

Any help is appreciated!


Solution

  • I figured it out (at least in Chrome)!

    In the Chrome console at the top left you can select the context in which the console is running and one of the options is the service worker.

    Once you are in there, you can over write the cache functions however you want. For my purposes, I just did self.caches.open = () => Promise.reject("Error").