Programming

Testing Browser JavaScript Completely

If you have been watching my GitHub, you may have noticed a new repository show up a little while ago, named Testing Browser Javascript Completely.

The short story about that repository is that the current collective body of understanding on how to build a robust, flexible, and useful testing ecosystem for client side JavaScript with the current suite of tools available in the JavaScript space is infinitesimally small. Getting a framework set up that tested things in the correct way took weeks of effort. If we added up the amount of time the team spent on this testing setup, it would be in the hundreds of person hours.

The long story

Needless to say, where I work we’re pretty serious when it comes to testing. We want not only to get it right, but to have it actually be right, and to have it give us the information we need to be able to make accurate decisions about our level of confidence in the code we produce.

Things are not always as they seem

The first large hurdle I hit on this journey was simply a language context issue. Naming things is hard. Today, on the web, “JavaScript Testing” means node.js. Almost exclusively, every time you read something recent about testing JavaScript, it’s not telling you how to test browser side frontend JavaScript. You have to look for “client side” or “browser side” or similar words indicating that what you are looking at is actually useful for testing frontend code.

The trick is — you don’t know that until you have gone too far. You’ll read things that say “Ok, install X, Y and Z with npm” and little do you know you’re already eating from the poisoned apple. If you are pulling your testing libraries (not support files, but the testing libraries themselves) you have already lost the war. First they get you to install Mocha and Chai with npm. Then you’re installing some abstraction layer that occupies globals.window to make your code think it’s running in a browser. This works right up until you start testing things that 100% expect to be running in a browser (like jQuery plugins). [Insert brick wall here.]

Testing code meant to run in a browser requires a browser

When it comes down to it, you need to be running your tests in a browser. But we’re also command line junkies and automating fools, so we have to satisfy our desire for testing against our desire to not repeatedly mash F5. Let’s be perfectly clear: refresh based testing is a valid testing strategy but it is definitely not a good one. I’d like to introduce you to my new friend PhantomJS. Now, PhantomJS and I get along fairly well, but figuring out the best way to talk to my good buddy caused us quite a bit of trouble. We started with grunt-phantomjs which worked right up until it didn’t. I don’t recall what the issue was at the time, but there was something specific with the Mocha wiring once we changed to browser side JavaScript testing that threw it for a loop. That’s when PhantomJS and I met grunt-mocha-phantomjs. It worked great! Ran the tests like a charm. Nice console output. You know… once we figured out the 15 steps to wire it all up nicely.

testing-browser-javascript-completely_—_bash_—_120×40-4

Now that we had a basis to work from, we needed to make sure we make sure we could use this specific setup with Jenkins, so we dug in and figured out that with only a minor change, we could also get it to output an XUnit formatted log file. Yay!

Now that we have the basics covered…

We’ve now hit our very basic bare minimum of acceptable tooling. We can write unit tests against any code we have, and run them in a CLI interface, quickly, to make sure we have not broken anything. Now we needed a way to make sure that we were writing unit tests against all of the code we had, so we would know that our changes hadn’t broken anything[Maestro, cue the music for the great code coverage war, please…]

Well, to be fair, it wasn’t actually a war. Let me rewind. Before we had realized we were doing everything wrong yet again, we had already gotten code coverage instrumented with Istanbul. If you are doing code coverage in JS and haven’t looked at Istanbulit’s about time you do. It provides an amazingly in-depth look at your code. Files, statement, branches, functions, and lines. This differs substantially to my experience with at least one other code coverage tool which we will talk about in a moment — I’ll get there, don’t worry. So we had Istanbul up and running, then we refactor everything to use PhantomJS and bam, code coverage is completely busted. Hacked and hacked to no avail, and then it dawned on me. Mocha (and by that virtue, Istanbul) was now running in the browser (ok, PhantomJS, but still…), which meant it could no longer write it’s data to the file system like it used to when we were running Mocha from the command line directly.

Needless to say, this made the chance of getting our wonderfully nice Istanbul coverage reports up and running again look a little bleak. Of course, at this point, we weren’t fully aware of how wonderfully nice Istanbul’s reports were. I took this opportunity to re-evaluate our tool selection for code coverage, and figured I would try out Blanket.js. It offered the promise of being simpler to instrument (no special step to modify the source code you want to coverage test), and you know, might actually work with our setup. Once we got it spun up I noticed something right away though: the coverage information was nowhere near as robust. Blanket.js offered just line based coverage information. Don’t get me wrong, line based coverage is better than no coverage, but I’d seen better. I wanted better. We had to have the better solution to solve this correctly.

We figured out that with a few little config changes we can make our test framework pull up the code we instrumented with Istanbul. It also turns out that that is about all Istanbul needs to “work”. Notice I said “work” and not work. We had code coverage running. We had the data. We could see it through DOM inspection when we loaded the test suite in our browser. Oh yeah! Did I forget to tell you that part? We could run ‘grunt test:browser’ and it opened our unit tests in our preferred browser for interactive unit testing! Now remember that browser bit, it’s about to come back to bite us in the. Come on, coverage data! Don’t be shy, come hide out on my hard drive where my tools can consume you. Maybe that didn’t help things — Anyway. So then someone (who is not me, and who will give me flack for not remembering they did it — I’ll come back and edit in the correct name of our brilliant genius once their name comes to light) figures out a way to pass the data back from the browser through PhantomJS to Grunt, and have it write said data to a file on the hard drive. Like I said, Genius.

And best of all

I’ve wrapped all of this knowledge up into a GitHub repository named Testing Browser Javascript Completely. This is essentially the same setup we use. There’s more goodies I want to add to it, but it has the basics all there and ready for you to start building. Did we make any big mistakes? Could we do things better? Send us a PR or open an issue! JavaScript testing is too important to be this hard to set up correctly. Let’s work together and keep an open conversation about how to make this easier. We all win when we have better go-to tooling. Making it easy to test JavaScript means it is more likely it will get tested.

Decoupling your Composer from GitHub and Packagist made easy

One of the things I learned very early on about working with a large team, is if you don’t make tasks as frictionless as possible, they have a tendency to not get done.

When we bought into using Behat as our functional testing framework of choice, it came with a mandate that when we install Behat, it cannot come from public sources. GitHub goes down. Repositories get updated. There have been instances of repos being compromised. We had to insulate ourselves from all of that risk. Fortunately, Composer has a GREAT tool for this, called Satis.

Satis provides you a way to create your own Packagist repository, complete with distributable tarballs of supported libraries. The one problem that I ran into (and this may have since been fixed? I’m not sure!) is that I couldn’t get it to download the dependencies of my dependencies. For example, your composer.json requires Package A which requires Package B. When I tried this, Satis would only build a repository with Package A. Knowing this would cause trouble down the line, I decided there had to be a way to make this simpler.

Out of that, Satis Repository Builder was born.

It will generate a Satis repository and upload it to S3 all with a single command. It’s still got some cleanup, but I realized that if I hadn’t done it in the last 4 months I wasn’t likely to do it in the next 4, and perhaps some feedback (or pull requests!) will spur some new life into my interest in it.

Once you have uploaded your repo, you can simply use the following setup in your composer.json to enforce pulling from your Satis repository:

That’s it! I’m sure there’s tons that can be done to the repo builder, and I look forward to seeing issues and pull requests!

One last piece of advice from the trenches

When you’re using a tool like this to decouple yourself from the risk of third party updates, be sure you are as specific as possible. List all of your dependencies and your dependencies dependencies, so when you do need to go back and upgrade a version of something, you control the scope of the update, not composer. The more specific you are in your composer.json about versions, the less variation you will see between builds of your satis repository.

Virtualizing your stack with Vagrant and Puppet (Redux)

I had the great fortune to be invited into the Columbus tech community and present my Virtualizing your stack with Vagrant and Puppet talk at the 2013 Columbus Code Camp. I had a blast, and if you’re reading this from Columbus, thank you for having such an awesome community.

This talk has been almost completely revamped since the last time I gave it. We walked through what Vagrant is, and how it relates to various virtual machine systems and cloud providers, and then forked from there to talk about how to use Puppet to create meaningful configuration of your servers.

Finally, and I think most importantly, we talked about how to sell the time investment to your superiors, and discussed the fact that if your development environment is not as close of a carbon copy of your production environment as possible, there is no clear way to verify that the code you have in your development environment will work at all once released to production.

(if the slides are not showing up, they may not have finished processing just yet)

Additional resources:

p.s. The video was reconstructed by manually taking the audio and combining them with the images in iMovie. If there’s something wrong, please let me know and I will work to correct it.

p.p.s. There is (somehow) a complete section on Hiera missing! I don’t know if I opened the wrong slide deck or what. The up side is there apparently wasn’t time to cover the material anyway, but the next time I give this talk it will be there.

Thinking to the future

What a ride this year has been, and it’s only slightly over half way done! I got a new job, and then we got acquired. While doing that, I’ve moved twice, each time carting loads of things from across the country. It’s been a heck of a year, and it’s nowhere near over.

All this change has made a few things exceptionally clear:

  1. My work very much fulfills me technically. In the last 5 months, I’ve worked on so many awesome projects. It’s left little time for other professional pursuits, however, which makes me sad, and I wish my friends at Point 5 Foundry well.
  2. On a related note, when I am not at work, I really just want to be with my family, relaxing, and having fun. Or, perhaps, if you’re an API fan, rest-ing.Yeah, ok, that was bad. I admit.
  3. If I was going to interrupt any of these two things, it would be spent being involved in the community. Our community is awesome! It’s not just our community though. I only have this for a career because of all of the hard work put in by so many really amazing people, so the least I can do is try and pay it forward.

So with that, there is one other announcement I am really excited to officially make (though, granted many of you may know by now, since I am so slow in writing this): I am now co-organizer of the San Francisco PHP User Group. Mike and I have already gotten one talk on the books, and we’re cooking up something extra special for September. I can’t wait until we can announce it.

Virtualize your stack with Vagrant and Puppet

Today we talked about working our way through the how and the why of using Vagrant and Puppet to virtualize our development environment, bringing our development environment in line with our production environment, and easy shortcuts to get something simple up and running without having to know the nitty-gritty details of Vagrant and Puppet.

For code reference on on this project, check out my GitHub repository.

Additional resources:

twitter