Exercise: A DSL for testing in JavaScript

October 12, 2016

Given an array:

var xs = [2, 4, 8, 16];

Write from scratch a library to check the values of elements within it:

forArray(xs).elemAt(3).shouldBe(equalTo(16));
// throws no exceptions

// forArray(xs).elemAt(2).shouldBe(equalTo(16));
// throws "expected 16 at index 2, but found 8"

// forArray(xs).elemAt(4).shouldBe(equalTo(16));
// throws "expected 16 at index 4, but the largest valid index is 3"