Skip to content

Instantly share code, notes, and snippets.

@wejrowski
Last active January 28, 2019 12:47
Show Gist options
  • Save wejrowski/cbf92b33db85d31eff86 to your computer and use it in GitHub Desktop.
Save wejrowski/cbf92b33db85d31eff86 to your computer and use it in GitHub Desktop.
Jasmine specs - custom console reporter
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>console test</title>
<link rel="shortcut icon" type="image/png" href="lib/jasmine-2.2.0/jasmine_favicon.png">
<link rel="stylesheet" href="lib/jasmine-2.2.0/jasmine.css">
<script src="lib/jasmine-2.2.0/jasmine.js"></script>
<script>
// Custom Reporter
// c.f. http://jasmine.github.io/2.2/custom_reporter.html
//
// This is a basic Jasmine setup for running test output in a browser console,
// hopefully to be used with Nashorn
// Append test details to this JSON output object
var finalReport = {
logs: [],
totalSpecs: 0,
stats: {}
};
var myReporter = (function () {
var reporter = {};
var log = function (str) {
console.log(str);
finalReport.logs.push(str);
}
reporter.jasmineStarted = function (suiteInfo) {
log('Running ' + suiteInfo.totalSpecsDefined + ' specs...');
log('');
finalReport.totalSpecs = suiteInfo.totalSpecsDefined;
};
reporter.suiteStarted = function (result) {
//result.fullName (same as description)
log(result.description);
};
reporter.specStarted = function (result) {
log(result.description);
};
reporter.specDone = function (result) {
finalReport.stats[result.status] = finalReport.stats[result.status] || 0
finalReport.stats[result.status] += 1;
log('=> ' + result.status);
for(var i = 0; i < result.failedExpectations.length; i++) {
log('Failure: ' + result.failedExpectations[i].message);
log(result.failedExpectations[i].stack);
}
};
// not useful:
//reporter.suiteDone = function (result) {
// log('=> ' + result.status);
// for(var i = 0; i < result.failedExpectations.length; i++) {
// log('AfterAll ' + result.failedExpectations[i].message);
// log(result.failedExpectations[i].stack);
// }
//};
reporter.jasmineDone = function () {
console.log("");
var finalOutput = finalReport.totalSpecs + ' examples';
Object.keys(finalReport.stats).forEach(function (specStatus) {
finalOutput += ', ' + finalReport.stats[specStatus] + ' ' + specStatus;
});
log(finalOutput);
}
return reporter;
}());
function extend(destination, source) {
for (var property in source) destination[property] = source[property];
return destination;
}
// Define Jasmine and attach globally
var jasmine = jasmineRequire.core(jasmineRequire);
jasmine.getEnv().addReporter(myReporter);
var jasmineInterface = jasmineRequire.interface(jasmine, jasmine.getEnv());
extend(this, jasmineInterface);
// Sample tests
describe('Dog', function() {
it('barks really loud', function() {
expect(1).toBe(1);
});
describe('when it has a bone', function() {
it('is happy', function() {
expect(true).toBe(true);
});
});
describe('when it eats chocolate', function() {
it('gets really sick', function() {
expect(true).toBe(true);
});
});
});
function runSpecs () {
// Runs the specs (must be called after specs are defined)
jasmine.getEnv().execute();
// print JSON report
console.log(JSON.stringify(finalReport));
}
runSpecs();
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment