duplication.js — TDD for Javascript

(Test-Driven Duplication)

duplication.js is a utility for building Javascript applications using the TDD methodology as practised by many agile teams. It tests whether code adheres to an (opinionated) specification, giving you the confidence that your code keeps working.

Example

Test specification tests/ellipsis_spec.js
var ellipsis = function(str, length) {
  length = length || 100;
  if (str.length <= length) return str;
  return str.substring(0, length-3) + '...';
};
Implementation src/ellipsis.js
var ellipsis = function(str, length) {
  length = length || 100;
  if (str.length <= length) return str;
  return str.substring(0, length-3) + '...';
};

Usage

First run npm install uglify-js in the application directory to install uglify-js. Then simply run ./duplication.sh src tests to run the tests, either manually or as part of a continuous integration setup. Download the script here.

Advantages

Ease of use. Most testing frameworks are overly complicated, making you write your tests in a format different from the actual implementation. duplication.js allows you to write the test in the same format as the implementation, which is much easier to write, and thus results in less bugs. With duplication.js you get all the benefits of a modern testing framework without the hassle.

TDD methodology. We encourage using the TDD methodology:

  1. Red — Write a specification on how the code should work, by implementing a working version. Verify that the test fails.
  2. Green — Implement the actual code. Verify that the test succeeds.
  3. Refactor — Refactor the code to improve readability, performance, etc.
When refactoring the code you should also refactor the test, in order to make it more readable and such.

Refactor with confidence. Since we use uglify.js to compile both sources and tests, you can easily refactor your code, knowing that your test coverage is always 100%. Changing variable names, improving whitespace formatting: these operations are most useful to improving code quality, and are therefore encouraged by duplication.js.

Superior performance. The method we employ to verify that the source matches the tests is much faster than traditional models that are based on code execution, while at the same time guaranteeing a perfect coverage rate.

Extensive language support. duplication.js supports testing of a wide variety of techniques, including asynchronous loading, Ajax calls, promises, WebGL, and infinite loops. It is even compatible with the widely used scaffolding tool cp.

Solves the halting problem. Computers are stupid, they cannot determine whether a program is going to run forever or not. Therefore traditional testing frameworks cannot test for this either. However, the vision behind duplication.js is human-centered testing: we trust you to sufficiently understand the specification, which means that you get full coverage for the halting problem, NP-complete algorithms, big data calculations, and so on, within a few milliseconds.

No more mocking and stubbing. Well, perhaps some mocking.

June 2013, Jan Paul Posma. Check out jsdares to learn real stuff. Discuss on Hacker News.