Unit Testing Browser Extensions

Uzair Shamim
Information & Technology
2 min readMay 21, 2018

This post has been migrated to my new blog that you can find here:

https://pureooze.com/blog/posts/2018-05-21-unit-testing-browser-extensions/

In April I became the maintainer of Saka, a browser extension that allows users to search through their tabs, bookmarks and history. The original goal of Saka was to provide an elegant tab search but this soon evolved to include recently closed tabs, bookmarks and history when the original maintainer eejdoowad recognized that users search for tabs the same way they search bookmarks and history. This was an important insight and it has helped make Saka a valuable productivity tool.

When I became the maintainer I was surprised at the absence of tests in the project. There were several components with complicated logic but no tests to be found anywhere. One of the most important things I have learned as a developer is that tests are the easiest ways to write reliable, easy to refactor code. Was the old maintainer just lazy? Did he simply not care about the quality of his code? No. The opposite in fact, he cared a lot.

Saka, a browser extension for searching tabs, recently closed, bookmarks and history.

The issue is that the lack of documentation on the topic means that almost no one is able to test their extension. Having no confidence in my ability to make changes without breaking the code, this was a big problem. But as fate would have it after trying a dozen different approaches I ended up finding a solution.

Why We Test

As developers we want to be sure that the code we write today is not going to become a burden to maintain later in the lifetime of the application. One way we avoid creating these burdens is by writing tests. The great thing about tests is that outside of just verifying the behavior of functions, tests allow us to provide documentation for future developers. For example by creating unit tests we declare the valid inputs and outputs for a given function. This makes it easier to refactor code because we can have confidence that our code is working correctly when all our tests pass.

The Testing Approach

This post will focus on setting up the environment and writing some basic unit tests. I have a separate post at this link which you can use to perform integration testing.

To read the rest of this post it can be found on my new blog here:

https://pureooze.com/blog/posts/2018-05-21-unit-testing-browser-extensions/

--

--