testRunnerReporter.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  6. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  7. var _createClass2 = require('babel-runtime/helpers/createClass');
  8. var _createClass3 = _interopRequireDefault(_createClass2);
  9. exports.default = testRunnerReporter;
  10. var _events = require('events');
  11. var _events2 = _interopRequireDefault(_events);
  12. var _chalk = require('chalk');
  13. var _chalk2 = _interopRequireDefault(_chalk);
  14. var _createStatsFormatter = require('../webpack/util/createStatsFormatter');
  15. var _createStatsFormatter2 = _interopRequireDefault(_createStatsFormatter);
  16. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17. var log = function log() {
  18. var _console;
  19. (_console = console).log.apply(_console, arguments); // eslint-disable-line no-console
  20. console.log(); // eslint-disable-line no-console
  21. };
  22. var formatTitleInfo = function formatTitleInfo(title) {
  23. return _chalk2.default.inverse('', title, '');
  24. };
  25. var formatTitleWarn = function formatTitleWarn(title) {
  26. return _chalk2.default.black.bgYellow('', title, '');
  27. };
  28. var formatTitleError = function formatTitleError(title) {
  29. return _chalk2.default.white.bold.bgRed('', title, '');
  30. };
  31. var Reporter = function () {
  32. function Reporter(options) {
  33. var _this = this;
  34. (0, _classCallCheck3.default)(this, Reporter);
  35. this.onUncaughtException = function (err) {
  36. log(formatTitleError('UNCAUGHT EXCEPTION'), 'Exception occurred after running tests');
  37. log(err.stack);
  38. };
  39. this.onLoadingException = function (err) {
  40. log(formatTitleError('RUNTIME EXCEPTION'), 'Exception occurred while loading your tests');
  41. log(err.stack);
  42. };
  43. this.onWebpackStart = function () {
  44. _this.clearConsole();
  45. if (_this.added.length > 0) {
  46. _this.logInfo(formatTitleInfo('MOCHA'), 'The following test entry files were added:');
  47. _this.logInfo(_this.added.map(function (f) {
  48. return '+ ' + f;
  49. }).join('\n'));
  50. }
  51. if (_this.removed.length > 0) {
  52. _this.logInfo(formatTitleInfo('MOCHA'), 'The following test entry files were removed:');
  53. _this.logInfo(_this.removed.map(function (f) {
  54. return '- ' + f;
  55. }).join('\n'));
  56. }
  57. _this.logInfo(formatTitleInfo('WEBPACK'), 'Compiling...');
  58. _this.added.length = 0;
  59. _this.removed.length = 0;
  60. };
  61. this.onWebpackReady = function (err, stats) {
  62. _this.clearConsole();
  63. if (stats != null) {
  64. var _formatStats = _this.formatStats(stats),
  65. _errors = _formatStats.errors,
  66. _warnings = _formatStats.warnings;
  67. if (_errors.length === 0 && _warnings.length === 0) {
  68. var startTime = stats.startTime,
  69. endTime = stats.endTime;
  70. var compileTime = endTime - startTime;
  71. _this.logInfo(formatTitleInfo('WEBPACK'), 'Compiled successfully in ' + _chalk2.default.green(compileTime + 'ms'));
  72. return;
  73. }
  74. if (_errors.length > 0) {
  75. Reporter.displayErrors('error', _errors);
  76. return;
  77. }
  78. if (_warnings.length > 0) {
  79. Reporter.displayErrors('warning', _warnings);
  80. }
  81. } else {
  82. Reporter.displayErrors('error', [err]);
  83. }
  84. };
  85. this.onMochaStart = function () {
  86. _this.logInfo(formatTitleInfo('MOCHA'), 'Testing...');
  87. };
  88. this.onMochaAbort = function () {
  89. _this.logInfo(formatTitleInfo('MOCHA'), 'Tests aborted');
  90. };
  91. this.onMochaReady = function (failures) {
  92. if (failures === 0) {
  93. _this.logInfo(formatTitleInfo('MOCHA'), 'Tests completed ' + _chalk2.default.green('successfully'));
  94. } else {
  95. _this.logInfo(formatTitleInfo('MOCHA'), 'Tests completed with ' + _chalk2.default.red(failures + ' failure(s)'));
  96. }
  97. };
  98. this.onEntryAdded = function (file) {
  99. _this.added.push(file);
  100. };
  101. this.onEntryRemoved = function (file) {
  102. _this.removed.push(file);
  103. };
  104. var eventEmitter = options.eventEmitter,
  105. interactive = options.interactive,
  106. quiet = options.quiet,
  107. cwd = options.cwd;
  108. this.added = [];
  109. this.removed = [];
  110. this.interactive = interactive;
  111. this.quiet = quiet;
  112. this.formatStats = (0, _createStatsFormatter2.default)(cwd);
  113. eventEmitter.on('uncaughtException', this.onUncaughtException);
  114. eventEmitter.on('exception', this.onLoadingException);
  115. eventEmitter.on('webpack:start', this.onWebpackStart);
  116. eventEmitter.on('webpack:ready', this.onWebpackReady);
  117. eventEmitter.on('mocha:begin', this.onMochaStart);
  118. eventEmitter.on('mocha:aborted', this.onMochaAbort);
  119. eventEmitter.on('mocha:finished', this.onMochaReady);
  120. eventEmitter.on('entry:added', this.onEntryAdded);
  121. eventEmitter.on('entry:removed', this.onEntryRemoved);
  122. }
  123. (0, _createClass3.default)(Reporter, [{
  124. key: 'logInfo',
  125. value: function logInfo() {
  126. if (!this.quiet) {
  127. log.apply(undefined, arguments);
  128. }
  129. }
  130. }, {
  131. key: 'clearConsole',
  132. value: function clearConsole() {
  133. if (this.interactive) {
  134. process.stdout.write(process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H');
  135. }
  136. }
  137. }], [{
  138. key: 'displayErrors',
  139. value: function displayErrors(severity, errors) {
  140. var errorCount = errors.length;
  141. var message = severity === 'error' ? 'Failed to compile with ' + _chalk2.default.red(errorCount + ' ' + severity + '(s)') : 'Compiled with ' + _chalk2.default.yellow(errorCount + ' ' + severity + '(s)');
  142. var titleColor = severity === 'error' ? formatTitleError : formatTitleWarn;
  143. log(titleColor('WEBPACK'), message);
  144. errors.forEach(function (err) {
  145. return log(err);
  146. });
  147. }
  148. }]);
  149. return Reporter;
  150. }();
  151. function testRunnerReporter(options) {
  152. new Reporter(options); // eslint-disable-line no-new
  153. }