Just to be clear here we are talking about JavaScript code that is intended to be run in a browser, if you intend to run your code in node.js then test it there and stop reading this blog post.

However if you plan to run your code in a browser it can be a bit more complex. In general running your code in a headless situation such as via a node.js test runner makes sense. But here is the issue, JS implementations are not all the same. While things are now much better than they were 10 years ago there is still the possibility that there could be differences in how different browsers run your code. This is doubly so if you are using more recent features in JavaScript or the DOM.

To be clear, as far as the user is concerned the statement “but the tests pass” is as useful as “but it works on my machine”, which is to say not at all!

Web applications have to run on a range of desktop web browsers including Chrome, Firefox, MS IE, MS Edge, Safari and Opera, as well as mobile browsers. So it makes sense to test them there, at least some of the time. If you have some bit of code that hits a strange bug in one of them it would be really nice to know that as you are developing the code.

I for one would feel really stupid if I shipped some code and then found that in browser X It does not work right and that if I ran my test suite in browser X it showed the fault! I’m sure it has happened to someone out there.

So how do you test on a range of browsers? First of all you will need to have all those browsers around, this can be done by one of several ways, the obvious one is to just download them all onto your workstation. But that may not work well, first of all if you are like most web developers you probably have a bunch of browser plugins installed that you don’t want in your test! Secondly if you are on a mac you won’t be able to install MS Edge or IE.

This is where virtual machines are your friend! Microsoft provides VMs for most versions of IE and EDGE. In addition you can get VM’s for Firefox, Chrome and Opera from the normal sources (Safari is harder).

In general I find that selenium scripts tend to be very fragile but you can easily create a selenium script that will open up the page with your tests on it and wait for it to tell you that it passed or failed without too much problem. If you do not feel like setting up your own selenium grid there are commercial services that will provide that for you.

Where do you run your tests? leave a comment and let me know!

I am going to be giving workshops on doing Unit and Property Based testing in Javascript in London, Tel Aviv and Be’er Sheva.

Other blogposts on this..