qc.js: QuickCheck for JavaScript

I’m really excited to release this to the world.

QuickCheck is a fairly new testing concept that came out of the Haskell community. The goal of QuickCheck is to save time when writing unit tests.

I wanted to try QuickCheck in JavaScript, and I was really surprised to find no one had done a port. So I gave it a whirl and I found it was less complicated than I thought.

Therefore, here is qc.js: QuickCheck for Javascript.

Firebug Console showing qc.js output.

Firebug Console showing qc.js output.

Here’s a quick synopsis of why you might want it:

When we write conventional unit tests we construct a series of test cases to try to exercise the majority of some bit of code.

QuickCheck changes the structure of this. Instead of individual test cases, we take our code and consider some aspect of it’s inputs. We then write functions which check properties of the output of arbitrary inputs to our code.

A property of an add function might be that given two arbitrary positive integers, the result should be larger than each of them.

A property of a sort function might be that given an arbitrary list of arbitrary integers and two different arbitrary indexes smaller than the length of the list, the element at the smaller list index should be smaller than the element at the larger list index. (woo mouthful)

So given a function like that, we can generate hundreds or thousands of arbitrary inputs and run our code and then the property function each time.

It’s fun to work with.

For some code, especially simple pure computation, it’s very quick and easy to write properties and you get good test coverage with less effort than the xUnit framework for your language.

John Hughes, one of the researchers who popularized this concept, has a video showing how to describe arbitrary inputs for normal impure code.

He shows how random unit testing can be creepy good at finding obscure corner case bugs and helping us understand the implications of the code we are writing.

I’ve got in the back of my mind to try using qc.js to throw arbitrary input events at a DOM/JS user interface to weed out weird bugs. At this point I have little more than a vague sense that it should be possible.

But anyway, enjoy.

My BitBucket repository.

John Hughes explains QuickCheck and demonstrates his Erlang implementation.

Explore posts in the same categories: Uncategorized

2 Comments on “qc.js: QuickCheck for JavaScript”


  1. Thanks, will check it out.


  2. […] Javascript version of QuickCheck exists, and we had a good use case. We use so-called reactive lists, that is, lists which can be […]


Leave a comment