Facebook's Jest javascript unit testing framework

Over the past few days i have been investigating Facebook's Jest - a javascript unit testing framework. I have found a number of issues with it for my use case.

My main issue is as to what it is actually useful for. The documentation outlines how to test microcosmically small sections of code. Admitedly, that is exactly what a unit test is, however I don't personally believe a lot of people build javascript apps with such minute seperation of concerns.

One of my projects uses React for a number of its client facing interfaces. Jest is used by Facebook to test their React components, so I thought I would try and do the same.

I can absolutely see how one could and would test a component. React is great in that you can make simple components and reuse them in multiple places. The example shown in the Jest documentation outlines how you could test a checkbox component - that really is the extent to which Jest shines in my opinion. I can test that when I change my <SelectButton /> components value my valueChanged callback is called, and an ajax request is sent off.

Integration

A React component can be made up of a number of other react components. In fact, a react interface is a number of react components layed out together and interacting with one another. As such, having tested each component individually, I would like to test the interface as a whole, and the interactions between the components. This is more of an integration test than a unit test, and Jest certainly is not designed for this.

Issues

Jest is extremely new, and is not particularly well documented at the moment. Within a reactive setup, it only works for the most basic of setups. For example, I build my code using gulp and browserify. I use browserify-shim to allow me to require() modules that I am loading from a CDN for example. Jest does not play with this well. You can work around this, but writing tests should not be hard or complex and I expect a testing framework to remove the need for complex boilerplate.

Another problem that I have encountered is the automocking functionality - in principle it is great but it does not work across the board. In certain situations you have to implement your own mocks, and who wants to do that :P It is also not immediately clear what is mocked and when - although the documentation makes it seem clear, I created a manual mock, and within it required some other modules which were not mocked. I then manually created a mock with jest.mock() but it broke the unit under test.

Conclusion

If you are testing extremely small, simple units, Jest is great. It is too immature to stand out to me, but I will certainly keep an eye on it. If I get the the time I would like to read into the internals some more - I feel that if you know exactly what it is doing, and how it is doing it you will get a lot more from it.

At the end of the day I use testing as a way of making me feel confident that my code works as I expect it to and does not regress. I think Jest is a nice compliment to a test suite but it certainly wouldn't be my first port of call.


Thomas Clowes

Thomas Clowes

I am a 28 year old software engineer from the United Kingdom. During the day I build multi platform applications. In my spare time I eat food and run marathons. Sometimes I write angry tweets.