Skip to content

Instantly share code, notes, and snippets.

@buren
Last active October 19, 2015 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buren/ba8bad40021e2d57832d to your computer and use it in GitHub Desktop.
Save buren/ba8bad40021e2d57832d to your computer and use it in GitHub Desktop.
Minimal JS test DSL
function describe(label, testFunction) {
return new test(label, testFunction);
}
function test(label, testFunction) {
var self = this;
self.testLabel = label;
self.itLabel;
var label = function() { return self.testLabel + ' ' + self.itLabel; };
var passed = function() { console.log(label(), true); };
var failed = function(result, expected) {
console.error(label(), 'Expected:', expected, 'but was', result);
};
self.it = function(label, itFunction) {
self.itLabel = label;
return itFunction(self.assert);
};
self.assert = function(result, expected) {
if (result === expected) {
passed();
} else {
failed(result, expected);
}
};
testFunction(self.it);
}
describe('describe', function(it) {
var emptyFunction = function() {};
var itFunction = function(it) {
it('it label', emptyFunction);
};
var assertFunction = function(it) {
it('', function(assert) {
assert(true, true);
})
};
it('has a label', function(assert) {
var label = 'a label';
var result = describe(label, emptyFunction)
assert(result.testLabel, label);
});
describe('it', function(it) {
it('has a label', function(assert) {
var result = describe('', itFunction);
assert(result.itLabel, 'it label');
});
});
describe('assert', function(it) {
it('should have assert function', function(assert) {
var result = describe('label', itFunction);
assert(result.assert instanceof Function, true);
});
});
});
describe('test', function(it) {
it('can assert true', function(assert) {
assert(true, true);
});
it('should really NOT work', function(assert) {
assert(false, true);
});
describe('nested describes', function(it) {
it('should work again', function(assert) {
assert(true, true);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment