reporter-agregator.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. var path = require('path');
  2. var cp = function(o){ if(typeof o != 'object') return o; var n = {}; for(var k in o) n[k] = o[k]; return n; };
  3. function Reporter(options){
  4. var onDone = options.onDone || function(){};
  5. /**
  6. * A reporter should implement the jasmine reporter interface
  7. * as well as the following properties:
  8. * - {string} format e.g. 'html'
  9. * - {function} getReport()
  10. * - {function} updateReport()
  11. * - {function} reset()
  12. */
  13. this._updateListeners = [];
  14. this.report = {};
  15. this._jasmineReporters = {};
  16. this._jasmineReporters['simpleReporter'] = require('./reporter-simple').create();
  17. this._jasmineReporters['junitReporter'] = require('./reporter-junit').create();
  18. this._jasmineReporters['htmlReporter'] = require('./reporter-html').create({
  19. jasminePath: path.normalize(__dirname+'/resources/jasmine.js'),
  20. jasmineHtmlPath: path.normalize(__dirname+'/resources/jasmine-html.js'),
  21. skeletonPath: path.normalize(__dirname+'/resources/skeleton.html')
  22. }, onDone);
  23. }
  24. Reporter.prototype._updateReport = function(){
  25. // tell reporters to update themselves
  26. for(var k in this._jasmineReporters){
  27. var reporter = this._jasmineReporters[k];
  28. reporter.updateReport();
  29. this.report[reporter.format] = reporter.getReport();
  30. }
  31. for(var i = 0; i < this._updateListeners.length; i++){
  32. this._updateListeners[i]( cp(this.report) );
  33. }
  34. };
  35. Reporter.prototype.getJasmineReporter = function(){
  36. var that = this;
  37. return {
  38. log : function(str){
  39. for(var k in that._jasmineReporters){
  40. var reporter = that._jasmineReporters[k];
  41. if(reporter.log) reporter.log(str);
  42. }
  43. },
  44. reportSpecStarting : function(runner) {
  45. for(var k in that._jasmineReporters){
  46. var reporter = that._jasmineReporters[k];
  47. if(reporter.reportSpecStarting) reporter.reportSpecStarting(runner);
  48. }
  49. },
  50. reportRunnerStarting : function(runner) {
  51. for(var k in that._jasmineReporters){
  52. var reporter = that._jasmineReporters[k];
  53. if(reporter.reportRunnerStarting) reporter.reportRunnerStarting(runner);
  54. }
  55. },
  56. reportSuiteResults : function(suite) {
  57. for(var k in that._jasmineReporters){
  58. var reporter = that._jasmineReporters[k];
  59. if(reporter.reportSuiteResults) reporter.reportSuiteResults(suite);
  60. }
  61. },
  62. reportSpecResults : function(spec) {
  63. for(var k in that._jasmineReporters){
  64. var reporter = that._jasmineReporters[k];
  65. if(reporter.reportSpecResults) reporter.reportSpecResults(spec);
  66. }
  67. },
  68. reportRunnerResults : function(runner) {
  69. for(var k in that._jasmineReporters){
  70. var reporter = that._jasmineReporters[k];
  71. if(reporter.reportRunnerResults) reporter.reportRunnerResults(runner);
  72. }
  73. that._updateReport();
  74. },
  75. reportStartingGroup : function(name){
  76. for(var k in that._jasmineReporters){
  77. var reporter = that._jasmineReporters[k];
  78. if(reporter.reportStartingGroup) reporter.reportStartingGroup(name);
  79. }
  80. }
  81. };
  82. };
  83. Reporter.prototype.onUpdate = function(callback){
  84. this._updateListeners.push(callback);
  85. };
  86. Reporter.prototype.getReport = function(){
  87. return this.report;
  88. };
  89. Reporter.prototype.reset = function(){
  90. for(var k in this._jasmineReporters){
  91. var reporter = this._jasmineReporters[k];
  92. reporter.reset();
  93. }
  94. }
  95. exports.create = function(options){
  96. var reporter = new Reporter(options);
  97. return reporter;
  98. };