notify_reporter.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _path() {
  7. const data = _interopRequireDefault(require('path'));
  8. _path = function _path() {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _util() {
  14. const data = _interopRequireDefault(require('util'));
  15. _util = function _util() {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _exit() {
  21. const data = _interopRequireDefault(require('exit'));
  22. _exit = function _exit() {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _nodeNotifier() {
  28. const data = _interopRequireDefault(require('node-notifier'));
  29. _nodeNotifier = function _nodeNotifier() {
  30. return data;
  31. };
  32. return data;
  33. }
  34. var _base_reporter = _interopRequireDefault(require('./base_reporter'));
  35. function _interopRequireDefault(obj) {
  36. return obj && obj.__esModule ? obj : {default: obj};
  37. }
  38. function _slicedToArray(arr, i) {
  39. return (
  40. _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest()
  41. );
  42. }
  43. function _nonIterableRest() {
  44. throw new TypeError('Invalid attempt to destructure non-iterable instance');
  45. }
  46. function _iterableToArrayLimit(arr, i) {
  47. var _arr = [];
  48. var _n = true;
  49. var _d = false;
  50. var _e = undefined;
  51. try {
  52. for (
  53. var _i = arr[Symbol.iterator](), _s;
  54. !(_n = (_s = _i.next()).done);
  55. _n = true
  56. ) {
  57. _arr.push(_s.value);
  58. if (i && _arr.length === i) break;
  59. }
  60. } catch (err) {
  61. _d = true;
  62. _e = err;
  63. } finally {
  64. try {
  65. if (!_n && _i['return'] != null) _i['return']();
  66. } finally {
  67. if (_d) throw _e;
  68. }
  69. }
  70. return _arr;
  71. }
  72. function _arrayWithHoles(arr) {
  73. if (Array.isArray(arr)) return arr;
  74. }
  75. function _defineProperty(obj, key, value) {
  76. if (key in obj) {
  77. Object.defineProperty(obj, key, {
  78. value: value,
  79. enumerable: true,
  80. configurable: true,
  81. writable: true
  82. });
  83. } else {
  84. obj[key] = value;
  85. }
  86. return obj;
  87. }
  88. const isDarwin = process.platform === 'darwin';
  89. const icon = _path().default.resolve(__dirname, '../assets/jest_logo.png');
  90. class NotifyReporter extends _base_reporter.default {
  91. constructor(globalConfig, startRun, context) {
  92. super();
  93. _defineProperty(this, '_startRun', void 0);
  94. _defineProperty(this, '_globalConfig', void 0);
  95. _defineProperty(this, '_context', void 0);
  96. this._globalConfig = globalConfig;
  97. this._startRun = startRun;
  98. this._context = context;
  99. }
  100. onRunComplete(contexts, result) {
  101. const success =
  102. result.numFailedTests === 0 && result.numRuntimeErrorTestSuites === 0;
  103. const firstContext = contexts.values().next();
  104. const hasteFS =
  105. firstContext && firstContext.value && firstContext.value.hasteFS;
  106. let packageName;
  107. if (hasteFS != null) {
  108. // assuming root package.json is the first one
  109. const _hasteFS$matchFiles = hasteFS.matchFiles('package.json'),
  110. _hasteFS$matchFiles2 = _slicedToArray(_hasteFS$matchFiles, 1),
  111. filePath = _hasteFS$matchFiles2[0];
  112. packageName =
  113. filePath != null
  114. ? hasteFS.getModuleName(filePath)
  115. : this._globalConfig.rootDir;
  116. } else {
  117. packageName = this._globalConfig.rootDir;
  118. }
  119. packageName = packageName != null ? `${packageName} - ` : '';
  120. const notifyMode = this._globalConfig.notifyMode;
  121. const statusChanged =
  122. this._context.previousSuccess !== success || this._context.firstRun;
  123. const testsHaveRun = result.numTotalTests !== 0;
  124. if (
  125. testsHaveRun &&
  126. success &&
  127. (notifyMode === 'always' ||
  128. notifyMode === 'success' ||
  129. notifyMode === 'success-change' ||
  130. (notifyMode === 'change' && statusChanged) ||
  131. (notifyMode === 'failure-change' && statusChanged))
  132. ) {
  133. const title = _util().default.format('%s%d%% Passed', packageName, 100);
  134. const message = _util().default.format(
  135. (isDarwin ? '\u2705 ' : '') + '%d tests passed',
  136. result.numPassedTests
  137. );
  138. _nodeNotifier().default.notify({
  139. icon,
  140. message,
  141. title
  142. });
  143. } else if (
  144. testsHaveRun &&
  145. !success &&
  146. (notifyMode === 'always' ||
  147. notifyMode === 'failure' ||
  148. notifyMode === 'failure-change' ||
  149. (notifyMode === 'change' && statusChanged) ||
  150. (notifyMode === 'success-change' && statusChanged))
  151. ) {
  152. const failed = result.numFailedTests / result.numTotalTests;
  153. const title = _util().default.format(
  154. '%s%d%% Failed',
  155. packageName,
  156. Math.ceil(Number.isNaN(failed) ? 0 : failed * 100)
  157. );
  158. const message = _util().default.format(
  159. (isDarwin ? '\u26D4\uFE0F ' : '') + '%d of %d tests failed',
  160. result.numFailedTests,
  161. result.numTotalTests
  162. );
  163. const watchMode = this._globalConfig.watch || this._globalConfig.watchAll;
  164. const restartAnswer = 'Run again';
  165. const quitAnswer = 'Exit tests';
  166. if (!watchMode) {
  167. _nodeNotifier().default.notify({
  168. icon,
  169. message,
  170. title
  171. });
  172. } else {
  173. _nodeNotifier().default.notify(
  174. {
  175. actions: [restartAnswer, quitAnswer],
  176. closeLabel: 'Close',
  177. icon,
  178. message,
  179. timeout: 10,
  180. title
  181. },
  182. (err, _, metadata) => {
  183. if (err || !metadata) {
  184. return;
  185. }
  186. if (metadata.activationValue === quitAnswer) {
  187. (0, _exit().default)(0);
  188. return;
  189. }
  190. if (metadata.activationValue === restartAnswer) {
  191. this._startRun(this._globalConfig);
  192. }
  193. }
  194. );
  195. }
  196. }
  197. this._context.previousSuccess = success;
  198. this._context.firstRun = false;
  199. }
  200. }
  201. exports.default = NotifyReporter;