Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

  • Blog Navigation
Screw.Unit, a new JS testing framework, version 0.1

Screw.Unit is a Behavior-Driven Testing Framework for Javascript written by Nathan Sobo and Nick Kallen. It features nested describes. Its goals are to provide:

  • a DSL for elegant, readable, organized specs;
  • an interactive runner which can execute focused specs and describes;
  • and brief, extensible source-code.

What it is

Test Runner

The testing language is inspired by JSpec (and Rspec, obviously). Consider,

describe("Matchers", function() {
  it("invokes the provided matcher on a call to expect", function() {
    expect(true).to(equal, true);
    expect(true).to_not(equal, false);

A key feature of Screw.Unit are nested describes and the cascading before behavior that entails:

describe("a nested describe", function() {
  var invocations = [];

  before(function() {

  describe("a doubly nested describe", function() {
    before(function() {
      invocations.push('inner before');

    it("runs befores in all ancestors prior to an it", function() {
      expect(invocations).to(equal, ["before", "inner before"]);

The Screw.Unit runner is pretty fancy, supporting focused describes and focused its:

Focused Runner

You can download the source from Github. Please see the included spec (screwunit_spec.js) to get up and running.

Implementation Details

Screw.Unit is implemented using some fancy metaprogramming learned from the formidable Yehuda Katz. This allows the describe and it functions to not pollute the global namespace. Essentially, we take the source code of your test and wrap it in a with block which provides a new scope:

var contents = fn.toString().match(/^[^{]*{((.*n*)*)}/m)[1];
var fn = new Function("matchers", "specifications",
  "with (specifications) { with (matchers) { " + contents + " } }"
);, Screw.Matchers, Screw.Specifications);

Furthermore, Screw.Unit is implemented using the Concrete Javascript style, which is made possible by the Effen plugin and jQuery. Concrete Javascript is an alternative to MVC. In Concrete Javascript, DOM objects serve as the model and view simultaneously. The DOM is constructed using semantic (and visual) markup, and behaviors are attached directly to DOM elements. For example,

  parent: function() {
    return $(this).parent('.describes').parent('.describe');
  run: function() {

Here two methods (#parent and #run) are attached directly to DOM elements that have class describe. To invoke one of these methods, simply:


Bind behaviors by passing a hash (see the previous example). Using CSS3 selectors and cascading to attach behaviors provides interesting kind of multiple inheritance and polymorphism:

$('.describe, .it').fn({...}); // applies to both describe and its
$('.describe .describe').fn({...}); // applies to nested describes only

A typical Concrete Javascript Application is divided into 4 aspects:

  • a DOM data model,
  • CSS bound to DOM elements,
  • asynchronous events bound to DOM elements (click, mouseover), etc.,
  • synchronous behaviors bound to DOM elements (run and parent in the above example).

The Concrete style is particularly well-suited to Screw.Unit; to add the ability to run a focused spec, we simply bind a click event to an it or a describe, which runs itself:

$('.describe, .it')
  .click(function() {

Anyway, more details about Effen / Concrete Javascript in a later post.


Screw.Unit is designed from the ground-up to be extensible. For example, to add custom logging, simply subscribe to certain events:

  .bind('enqueued', function() {...})
  .bind('running', function() {...})
  .bind('passed', function() {...})
  .bind('failed', function(e, reason) {...})

Thanks to

  • Nathan Sobo
  • Yehuda Katz
  • Brian Takita

    Very nice. The JsSpec runner can also be (easily?) adapted to support Screw.Unit. I’d like to help out with that.

  • Brian Takita

    I mean the JsSpec server, which could use a blog post too. :)

  • So the new thing on js world is to copy ruby?

    rspec all over

    You guys should learn ruby instead.

  • Steve C

    Great point Nuno we’ll get right on that.

  • Can I sense some kind of irony in that post? :P

  • Dav

    Nice! Looking forward to trying it out.

    Love the name too.

  • qwe

    Prototype already has very similar testing framework (made a few years back). It would be great see what’s different from it, except Screw.Unit is jQuery based, for us to get it started and get excited about it.

  • nick kallen

    qwe – prototype has a testing framework, and it supports bdd style assertions. there is also jspec and jsspec, both javascript bdd style testing frameworks.

    jspec and screw.unit bear some similarity in their closure-based testing techniques which is fundamentally different and much more flexible and powerful than the hash technique of jsspec and prototype. screw.unit features, in particular, nested cascading describes which is imo one of the killer features of rspec; if bdd is just about saying “should” rather than “assert”, then bdd is nothing at all.

    Screw.Unit is designed to be the testing framework for advanced javascript users and testers. If you use rspec you know how important nested describes are. If you are a sophisticated javascript user, you know how important closures are for idiomatic javascript programming. In general, I have very little nice to say about prototype so I’ll stop talking now.

  • There is also which is a clone of prototypejs’ test framework without the prototype dependencies.

  • Hey hey, just letting everyone know about some great recent advances in JSpec, moving ahead approaching a 1.0 release, lots of new goodies !

Share This