Integration Testing Browser Extensions with Jest
Tags: javascript, testing, chrome, jest
Published: 2018-08-18 02:44:53

---
title: Integration Testing Browser Extensions with Jest
published: true
description: Integration testing can be used to validate the correctness of the code we write. In this post I cover how to setup Jest and sinon-chrome to test React and Preact components in browser extensions.
tags: JavaScript, Testing, Chrome, Jest
---

Previously I wrote about how [I became the maintainer of Saka](https://github.com/lusakasa/saka), an open source browser extension that allows users to search through and load open tabs, browsing history and bookmarks. [I talked about how I came up with a solution for unit testing the extension](https://dev.to/pureooze/unit-testing-browser-extensions-39a5) to give me confidence with code changes. I also mentioned that there were issues with integration testing that I ran into which made it difficult to test components that relied on browser APIs.

Today I am happy to report that I have found a way to perform integration testing on extensions and want to share it with you in this post. But before we go down that particular rabbit hole lets first discuss integration testing and why it is useful for validating software.

## The Testing Trophy
![The Testing Trophy](https://thepracticaldev.s3.amazonaws.com/i/xqzp0ej2grrku8dxex96.jpg)

[Kent C. Dodds has written about something he calls the 'Testing Trophy'](https://blog.kentcdodds.com/write-tests-not-too-many-mostly-integration-5e8c7fff591c). If you have heard of [the testing pyramid](https://martinfowler.com/articles/practical-test-pyramid.html) before this is a similar concept - it's a visualization of how you should prioritize the different types of testing in applications. The title of Kent's post says it all:

> Write tests. Not too many. Mostly integration.

Why does he say this? Kent notes the problem with unit tests is that they only prove individual units work as expected- they do not prove that the units can work together as a whole. Integration testing on the other hand proves that all the components in our project can actually work together as expected.

## The Need For Integration Testing
Let's leave the world of software and look at a real world example. Suppose we wanted to build a sink for a bathroom. There are 4components to this sink: the faucet, the basin, the drainage system and the water line. Since the drain and water line come with the building we only need to worry about adding the faucet and the basin.
We go to the store and pick a faucet and basin that we like. We bring them on site and assemble each individually. We confirm that the faucet and basin each work as expected and that they have no defects. Finally we assemble the full sink - hooking up the faucet to the water line and the basin to the drainage. After all our labor we are excited to see our sink in action so we turn on the faucet and what happens? Well…

![Source: X Unit Tests, 0 Integration Tests](https://thepracticaldev.s3.amazonaws.com/i/zbqnfbjkl7p67met4sus.gif)

Oops! While we did check to see that the faucet and basin work on their own we forgot to check if the two were actually compatible. This is why integration testing is valuable — it proves that different components, modules and libraries work together as expected.

[Kent C. Dodds — Write tests. Not too many. Mostly integration.](https://blog.kentcdodds.com/write-tests-not-too-many-mostly-integration-5e8c7fff591c)

[Ulrika Malmgren — X Unit Tests, 0 Integration Tests](https://natooktesting.wordpress.com/2017/08/24/x-unit-tests-0-integration-tests/)

# Solution
Since writing my last post I have managed to get Jest working with Preact, the framework used to create Saka. Jest is a modern testing framework that can run in Node or JSDOM. I will also be using the dom-testing-library to perform the rendering and assertions on my components.

Just keep in mind that while my specific solutions will be tailored for Preact, they will still work for other frameworks — especially React — with slight modifications for framework specific libraries.

There is an example Preact extension with Jest setup for reference here: [https://github.com/pureooze/extension-testing-example](https://github.com/pureooze/extension-testing-example)

## Installation
> You don’t need to install the preact packages if you use a different framework

First you need to install the required dependencies:
```bash
yarn add --dev babel-jest babel-plugin-transform-class-properties babel-plugin-transform-react-jsx babel-preset-env babel-preset-react jest sinon-chrome
```

If you are using Preact you need to also run the following:
```bash
yarn add --dev preact-compat preact-render-to-string preact-test-utils preact-testing-library
```

Note that just like in the previous post we will be using sinon-chrome to mock all browser APIs.

## Configuring Jest
**_For Preact only, not required for React._**

With Jest installed you now need to create a config to tell jest how to deal with parsing Preact. If you use another framework like React you don’t need to do this. Create a jest.config.js file in the root directory of your project with the following contents:

{% gist https://gist.github.com/pureooze/4302bc0450ac39f7a94d82c65e22338c %}

Notice the transform property is telling Jest to apply a custom transformer on all JSX files. To make it work we also need to create a jest.transform.js file:

{% gist https://gist.github.com/pureooze/c14b2b12bab8c7db8cb5afa8dc02c944 %}

## Create The Commands
Add the following npm scripts to your package.json so that jest can be run from the command line:

{% gist https://gist.github.com/pureooze/cedca49990753995d4c84c1f78a20f33 %}

## Create The First Test
Jest is smart enough to scan your project and run any files it finds with the .test.js extension. Create a new file called Main.test.js in the tests directory of your project with the following contents where the import Main is the component you want to test:

{% gist https://gist.github.com/pureooze/0c180f20ef0f7659dbf6e26c53bfeec1 %}

Once the file is created, open a terminal in the root of your project and run the command:

```bash
yarn test:watch
```

You should see jest output something similar to this (assuming the test you created passed):

![Snapshots successfully taken](https://thepracticaldev.s3.amazonaws.com/i/fpxhxoqfebwq7bjiyrci.png)

Congrats! Jest has successfully run and created a snapshot for the test. Jest has created a *__snapshots__* directory where all the snapshots are stored. If you open one of them you will see something like this:

{% gist https://gist.github.com/pureooze/64802febb5b321f1cebebb64a4a67b95 %}

The next time the test runs, Jest will compare the existing snapshot to the snapshot it takes at runtime and notify you if a difference is detected.

# Conclusion
Integration testing is valuable when we want to assert the compatibility of the components we create. As Kent C. Dodds writes:

> Write tests. Not too many. Mostly integration.

You can use Jest to achieve this for modern Javascript projects and browser extensions are no different. With the help of sinon-chrome you can mock out any extension APIs that are used by your components.

**_If you liked this post be sure to [follow this blog](https://dev.to/pureooze) and [follow me on twitter](https://twitter.com/pureooze)._**

**_P.S.: Looking to contribute to an open source project? Come contribute to Saka, we could use the help! You can find the project here: [https://github.com/lusakasa/saka](https://github.com/lusakasa/saka)_**