Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. SERVICE WORKER TESTING
  2.  
  3. - SW’s have states:
  4. - Installing ( synonymous with registering)
  5. - Installed ( synonymous with registered)
  6. - Activating
  7. - Activated
  8. - Redundant
  9.  
  10. - We add eventListeners (as you would on browser elements) to detect and act upon changes in worker state
  11.  
  12. - SW’s must be registered before they can be installed and go active. They stay registered until their SW file changes or it’s explicitly de-registered (usually by calling the unregister() method on the SW)
.
  13.  
  14. - A SW’s cache (usually IndexedDB these days, although I see references to WebSQL occasionally) persists, even through de-registration.
  15.  
  16. To maintain a consistent testing environment, you must
 purge the cache and de-register the SW.
  17.  
  18. Generally, SW behavioral tests do one of two things:
  19. - listen for changes in SW state and report on them
  20. - read messages posted from the SW and report on them or the cache
  21.  
  22. This is all well and good if we want to test the BEHAVIOR of a SW - but what if we’re interesting in testing the stuff that’s going on inside of it?

  23.  
  24. - Here’s the workflow for unit testing using Service Workers:
  25. - Register your service worker.
  26. - Import a testing suite using importScripts()
  27. - Within the service worker, write tests.
  28. - Instead of your testing suite writing stuff to the Dom/console, configure it such that testing results are posted to the parent (using .postMessage() ).
  29. - Write a function that tells the service worker to run tests on command.
  30. - Outside of the SW write a test that listens for messages from that service worker.
  31. - Outside of the SW, write a function that commands the SW to run tests.
  32.  
  33. You can also configure create fake events and pass them to a service worker for testing purposes. See here for a list of events:
  34. https://developer.mozilla.org/en-US/docs/Web/API/Event
  35.  
  36. I don’t like this process too much for two reasons -
  37. - You’re writing a test that’s basically waiting for the results of a test, which feels unintuitive.
  38. - Managing the active/idle/terminated behaviors is a bit of a pain
  39. 
There are some particulars to keep in mind - Chrome generally tries to terminate up service worker threads after 30 seconds for efficiency. You may not want this in a testing environment - so to prevent this behavior, you have to turn on dev tools (possibly not desirable in a testing situation).
  40.  
  41. So the idea is: write a library that would allow you to write tests outside of the service worker environment and allow you to importScript() them into the SW, taking snapshots of the cache and de/registering along the way as needed.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement