Loose Bits Thoughts on distributed systems, cloud computing, and the intersection of law and technology.

Making JavaScript Tests Fast, Easy, & Friendly

Cross-posted from the Formidable Labs blog.

A few weeks ago, I presented a talk on “Making JavaScript Tests Fast, Easy, & Friendly” at the CascasdiaJS 2014 conference in Portland, OR. The talk looks beyond the traditional technical details of wiring up test frameworks and focuses on creating processes and environments that help developers actually write and run application tests.

Making the Tests Faster, Easier, Friendlier

The motivation for the talk comes from our work with developer teams of various sizes at Formidable Labs, where we have observed the challenges, successes, and failures of test development as a part of the overall development workflow. Essentially, these experiences can be culled down to the following:

  • If tests aren’t easy to write, then developers won’t write them.
  • If tests aren’t fast to run, then developers won’t run them.
  • If the organizational culture isn’t test-friendly, then developers won’t write or run the tests (and they’ll probably be unhappy to boot).

The talk dives into some tips and tricks to address these three broad goals as follows:


chai-jq - jQuery Assertions for Chai

A New jQuery Plugin for Chai

Chai is a wonderful JavaScript test assertion library that I rely on extensively (usually using it in conjunction with the Mocha test framework library). Chai has an intuitive, natural-language approach to assertions that enables you tests to read almost like an English narrative. Chai also supports plugins which extend the base assertion API.

Chai is quite often used for frontend JavaScript testing, and specifically for testing jQuery elements in an application web page. There is an existing chai-jquery plugin for Chai, which is extensive and really quite neat. Unfortunately, it has a few issues with overriding built-in Chai assertions like have and length in ways that changes the underlying Chai API.

With that motivation in mind, I hacked together a quick jQuery plugin, chai-jq for Chai that has a separate $-prefixed namespace to avoid collisions with existing Chai assertions, and put the project up on GitHub.

The chai-jq Plugin

The chai-jq plugin has full documentation at the project website, including installation instructions. The plugin works in all of the following environments:

  • Browser: Via a standard <script> tag include.
  • Browser + AMD: Via an AMD library like RequireJS.
  • Node.js + JsDom: In Node.js using the JsDom browser environment emulator.

In terms of what chai-jq provides, here is a brief tour of the API (stolen from the project docs):


Writing a Technical Book, Part 2 - Authoring

Writing a Book

This is the second in a series of posts on my experiences writing my first technical book, Backbone.js Testing, published in July, 2013. In this article, I reflect on the task of authoring the core of the book - from writing the first pages to finishing off the last round of technical edits and submission of the full draft of the book to my publisher.

A rough overview of salient tips and experiences from this part of the book-writing journey includes:

  • Make sure to have a reliable backup of your work - expect to lose all your files (and your computer).
  • Before writing anything, plan and outline everything.
  • The writing process will take much longer than you think it will.
  • The writing process is also tiring, stressful, and boring.
  • Technical review will bruise your ego and leave you with a much better book.
  • Once you get most of the way through the drafting process, you (the author) hold the power in your relationship with your publisher.

Where we Left Off

In my previous post on starting a book project, I discussed how I got roped into writing a technical book and the process from initial contact from the publisher to signing a book contract. As part of the negotiations, we agreed on the following ultimate chapter outline (with estimated page count and due dates):

  1. Setting up a Test Infrastructure (10 pages) - Jan. 8, 2013
  2. Creating a Backbone.js Application Test Plan (8 pages) - Jan. 16, 2013
  3. Test Assertions, Specs, and Suites (20 pages) - Jan. 28, 2013
  4. Test Spies (7 pages) - Feb. 4, 2013
  5. Test Stubs and Mocks (15 pages) - Feb. 11, 2013
  6. Headless Web Testing (12 pages) - Feb. 19, 2013
  7. Appendix A: Other JavaScript Test Frameworks (8 pages) - Feb. 25, 2013

As a spoiler for the rest of this post - I came nowhere near making most of the chapter deadlines.

Starting on the Book

After signing the contract in mid-December, I found that I suddenly had my first chapter due Jan. 8 and subsequent chapters following quickly after that. Shortly thereafter, I dug in and started getting my authoring and coding tools set up, enhancing my book outline, and dove into writing the first chapter.