Back to Resources

Blog

Posted July 22, 2013

Mocha Sauce for JavaScript Unit Tests, Created by Paul Bakaus

quote

Paul Bakaus, CTO of Studio 5 at Zynga, and a prodigious doer of things who created jQuery UI and the Aves Engine, created Mocha Sauce for use by the Zynga development team, and he was kind enough to write a little about it. Read on to learn about why he created Mocha Sauce. You can find Mocha Sauce on Github. Mocha Sauce is a bleeding edge project that connects Mocha with Sauce Labs. It started as a fork of mocha-cloud (a project that unfortunately doesn’t seem to be maintained any longer), but now lives on its own and comes with lots of additional features and improvements. It’s definitely not stable, but it is being used in production for an unannounced game here within Studio 5 at Zynga, and we like it so far!

 

So why in the world did I create it? Read on.

Requirements, and trying not to reinvent the wheel

My team and I needed our unit tests to run in the browser (during development), as a pre-commit hook (running in a headless browser), in multiple remote VMs / browsers, and finally, we needed them to be integrated with Jenkinsfor continuous integration. Based on these requirements, we were planning to reuse as many existing libraries or frameworks as possible, and we arrived at the following set of third party dependencies:

Mocha for actual unit testing, allowing me to run my unit tests in the browser. Mocha is very feature-rich and modular, and doesn’t enforce a particular loading or coding style. Since testing environments are always subject to environmental changes (per project and company), being modular and non-enforcing is essential. Mocha also has been around for a while, and there’s quite a bit of community around it. mocha-phantomjs to run the same Mocha test site in a terminal using PhantomJS, which made it trivial to implement a pre-commit hook that runs the test suite. mocha-phantomjs is amazing, as it supports a large number of Mocha reporters for output. Sauce Labs is an amazing service that allows you to automatically instantiate a pristine remote virtual machine that runs a specific browser on a specific platform. With WebDriver on node.js, you can spin up a VM and then instrument the remote browser using the WebDriver API. It’s really simple and definitely worth the money.

The missing link

But now that we can spin up remote VMs through node.js, how do we run our unit test suite in that VM, report back its output and collect it somewhere? This is where Mocha Sauce comes in. It builds on the excellent, but discontinued work of mocha-cloud and mocha-cloud-grid-view and adds many advanced features – we, for instance, needed to be able to use Sauce Connect to create a local tunnel, which then allows you to test firewalled or local urls in the VMs, and we also needed to integrate it with Jenkins, amongst many other smaller things. I could copy all of its documentation and paste it here but that would be dumb. I encourage you to simply read into the docs to get an idea about its features and usage. There’s also in-depth information of how to set it up. Keep in mind that this is alpha code, and probably will break here and there. If you do have requirements similar to mine, give it a shot and submit lots of feedback via Twitter to @pbakaus or email to pbakaus @ zynga. Now test your units!

Published:
Jul 22, 2013
Share this post
Copy Share Link
© 2023 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.