Skip to content

Instantly share code, notes, and snippets.

@ButuzGOL
Last active August 29, 2015 14:04
Show Gist options
  • Save ButuzGOL/ffcf9923dcdd13cd5b0d to your computer and use it in GitHub Desktop.
Save ButuzGOL/ffcf9923dcdd13cd5b0d to your computer and use it in GitHub Desktop.
Simple test framework
(function(global) {
'use strict';
var TestSuite = {},
Reporter,
specifications = {},
assert = {};
// Reporter
Reporter = function(options) {
this.options = options;
};
Reporter.prototype.buidLog = function(scenario) {
var container = document.getElementById(this.options.containerId),
passes = 0, failures = 0,
moduleTitle, testTitle,
moduleTitleResult,
testsContainer, testContainer,
testResult, testMessage;
passes = scenario.tests.filter(function(test) {
return test.success === true;
}).length;
failures = scenario.tests.length - passes;
moduleTitle = document.createElement('h2');
moduleTitle.innerText = scenario.name;
moduleTitleResult = document.createElement('span');
moduleTitleResult.innerText =
' (passes: ' + passes + ' failures: ' + failures + ')';
moduleTitle.appendChild(moduleTitleResult);
testsContainer = document.createElement('ul');
scenario.tests.forEach(function(test) {
testContainer = document.createElement('li');
testTitle = document.createElement('h4');
testTitle.innerText = test.name;
testContainer.appendChild(testTitle);
testResult = document.createElement('span');
if (test.success) {
testResult.style.color = 'green';
testResult.innerText = ' (success)';
} else {
testResult.style.color = 'red';
testResult.innerText = ' (fail)';
}
testTitle.appendChild(testResult);
if (!test.success) {
testMessage = document.createElement('p');
testMessage.style.color = 'red';
testMessage.innerText = test.error +
(test.stack ? ('\n' + test.stack) : '');
testContainer.appendChild(testMessage);
}
testsContainer.appendChild(testContainer);
});
container.appendChild(moduleTitle);
container.appendChild(testsContainer);
};
// TestSuite
TestSuite.scenario = {};
TestSuite.reporter = new Reporter({
containerId: 'results'
});
TestSuite.module = function(name, fn) {
this.scenario = {
name: name,
tests: []
};
fn(specifications);
specifications.run();
this.reporter.buidLog(this.scenario);
};
// Specifications
specifications.tests = [];
specifications.beforeFn = null;
specifications.beforeEachFn = null;
specifications.test = function(name, fn) {
specifications.tests.push({ name: name, fn: fn });
};
specifications.before = function(fn) {
this.beforeFn = fn;
};
specifications.beforeEach = function(fn) {
this.beforeEachFn = fn;
};
specifications.run = function() {
var _this = this;
if (this.beforeFn) {
this.beforeFn();
}
this.tests.forEach(function(test) {
var currentTestScenario = {
name: test.name
};
if (_this.beforeEachFn) {
_this.beforeEachFn();
}
try {
test.fn(assert);
currentTestScenario.success = true;
} catch(e) {
currentTestScenario.success = false;
currentTestScenario.error = e.name + ': ' + e.message;
currentTestScenario.stack = e.stack;
}
TestSuite.scenario.tests.push(currentTestScenario);
});
this.tests = [];
};
// Assert
assert.equals = function(expected, actual, message) {
if (expected === actual) {
return true;
}
this.fail('expected ' + expected + ' to equal ' + actual, message);
};
assert.notEquals = function(expected, actual, message) {
if (expected !== actual) {
return true;
}
this.fail('expected ' + expected + ' to not equal ' + actual, message);
};
assert.fail = function(reason, message) {
var err = new Error();
throw {
name: 'AssertionError',
message: reason + (message ? (': ' + message) : ''),
stack: err.stack.split('\n').splice(3).join('\n')
};
};
global.TestSuite = TestSuite;
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment