adequately good

decent programming advice

written by ben cherry



all articles filed under testing

Writing Testable JavaScript

The engineering culture at Twitter requires tests. Lots of tests. I haven't had formal experience with JavaScript testing before Twitter, so I've been learning a lot as I go. In particular, a number of patterns I used to use, write about, and encourage have turned out to be bad for writing testable code. So I thought it would be worthwhile to share a few of the ...

Spying Constructors in JavaScript

When writing unit-tests for code, a common technique is spying, where you set expectations on a method's invocation, run some code, and verify that the method was invoked as expected. This is pretty straightforward. Here's a simple example using JsMockito:

function foo(a) { return a; }
foo = spy(foo);
foo(1);
verify(foo)(1); // verified!
verify(foo)(2); // never run

Here, we're spying on the foo ...