NotifyReporter.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function path() {
  7. const data = _interopRequireWildcard(require('path'));
  8. path = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function util() {
  14. const data = _interopRequireWildcard(require('util'));
  15. util = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _exit() {
  21. const data = _interopRequireDefault(require('exit'));
  22. _exit = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _jestUtil() {
  28. const data = require('jest-util');
  29. _jestUtil = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. var _BaseReporter = _interopRequireDefault(require('./BaseReporter'));
  35. function _interopRequireDefault(obj) {
  36. return obj && obj.__esModule ? obj : {default: obj};
  37. }
  38. function _getRequireWildcardCache(nodeInterop) {
  39. if (typeof WeakMap !== 'function') return null;
  40. var cacheBabelInterop = new WeakMap();
  41. var cacheNodeInterop = new WeakMap();
  42. return (_getRequireWildcardCache = function (nodeInterop) {
  43. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  44. })(nodeInterop);
  45. }
  46. function _interopRequireWildcard(obj, nodeInterop) {
  47. if (!nodeInterop && obj && obj.__esModule) {
  48. return obj;
  49. }
  50. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  51. return {default: obj};
  52. }
  53. var cache = _getRequireWildcardCache(nodeInterop);
  54. if (cache && cache.has(obj)) {
  55. return cache.get(obj);
  56. }
  57. var newObj = {};
  58. var hasPropertyDescriptor =
  59. Object.defineProperty && Object.getOwnPropertyDescriptor;
  60. for (var key in obj) {
  61. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  62. var desc = hasPropertyDescriptor
  63. ? Object.getOwnPropertyDescriptor(obj, key)
  64. : null;
  65. if (desc && (desc.get || desc.set)) {
  66. Object.defineProperty(newObj, key, desc);
  67. } else {
  68. newObj[key] = obj[key];
  69. }
  70. }
  71. }
  72. newObj.default = obj;
  73. if (cache) {
  74. cache.set(obj, newObj);
  75. }
  76. return newObj;
  77. }
  78. function _defineProperty(obj, key, value) {
  79. if (key in obj) {
  80. Object.defineProperty(obj, key, {
  81. value: value,
  82. enumerable: true,
  83. configurable: true,
  84. writable: true
  85. });
  86. } else {
  87. obj[key] = value;
  88. }
  89. return obj;
  90. }
  91. const isDarwin = process.platform === 'darwin';
  92. const icon = path().resolve(__dirname, '../assets/jest_logo.png');
  93. class NotifyReporter extends _BaseReporter.default {
  94. constructor(globalConfig, startRun, context) {
  95. super();
  96. _defineProperty(this, '_notifier', loadNotifier());
  97. _defineProperty(this, '_startRun', void 0);
  98. _defineProperty(this, '_globalConfig', void 0);
  99. _defineProperty(this, '_context', void 0);
  100. this._globalConfig = globalConfig;
  101. this._startRun = startRun;
  102. this._context = context;
  103. }
  104. onRunComplete(contexts, result) {
  105. const success =
  106. result.numFailedTests === 0 && result.numRuntimeErrorTestSuites === 0;
  107. const firstContext = contexts.values().next();
  108. const hasteFS =
  109. firstContext && firstContext.value && firstContext.value.hasteFS;
  110. let packageName;
  111. if (hasteFS != null) {
  112. // assuming root package.json is the first one
  113. const [filePath] = hasteFS.matchFiles('package.json');
  114. packageName =
  115. filePath != null
  116. ? hasteFS.getModuleName(filePath)
  117. : this._globalConfig.rootDir;
  118. } else {
  119. packageName = this._globalConfig.rootDir;
  120. }
  121. packageName = packageName != null ? `${packageName} - ` : '';
  122. const notifyMode = this._globalConfig.notifyMode;
  123. const statusChanged =
  124. this._context.previousSuccess !== success || this._context.firstRun;
  125. const testsHaveRun = result.numTotalTests !== 0;
  126. if (
  127. testsHaveRun &&
  128. success &&
  129. (notifyMode === 'always' ||
  130. notifyMode === 'success' ||
  131. notifyMode === 'success-change' ||
  132. (notifyMode === 'change' && statusChanged) ||
  133. (notifyMode === 'failure-change' && statusChanged))
  134. ) {
  135. const title = util().format('%s%d%% Passed', packageName, 100);
  136. const message = `${isDarwin ? '\u2705 ' : ''}${(0, _jestUtil().pluralize)(
  137. 'test',
  138. result.numPassedTests
  139. )} passed`;
  140. this._notifier.notify({
  141. icon,
  142. message,
  143. timeout: false,
  144. title
  145. });
  146. } else if (
  147. testsHaveRun &&
  148. !success &&
  149. (notifyMode === 'always' ||
  150. notifyMode === 'failure' ||
  151. notifyMode === 'failure-change' ||
  152. (notifyMode === 'change' && statusChanged) ||
  153. (notifyMode === 'success-change' && statusChanged))
  154. ) {
  155. const failed = result.numFailedTests / result.numTotalTests;
  156. const title = util().format(
  157. '%s%d%% Failed',
  158. packageName,
  159. Math.ceil(Number.isNaN(failed) ? 0 : failed * 100)
  160. );
  161. const message = util().format(
  162. (isDarwin ? '\u26D4\uFE0F ' : '') + '%d of %d tests failed',
  163. result.numFailedTests,
  164. result.numTotalTests
  165. );
  166. const watchMode = this._globalConfig.watch || this._globalConfig.watchAll;
  167. const restartAnswer = 'Run again';
  168. const quitAnswer = 'Exit tests';
  169. if (!watchMode) {
  170. this._notifier.notify({
  171. icon,
  172. message,
  173. timeout: false,
  174. title
  175. });
  176. } else {
  177. this._notifier.notify(
  178. {
  179. actions: [restartAnswer, quitAnswer],
  180. closeLabel: 'Close',
  181. icon,
  182. message,
  183. timeout: false,
  184. title
  185. },
  186. (err, _, metadata) => {
  187. if (err || !metadata) {
  188. return;
  189. }
  190. if (metadata.activationValue === quitAnswer) {
  191. (0, _exit().default)(0);
  192. return;
  193. }
  194. if (metadata.activationValue === restartAnswer) {
  195. this._startRun(this._globalConfig);
  196. }
  197. }
  198. );
  199. }
  200. }
  201. this._context.previousSuccess = success;
  202. this._context.firstRun = false;
  203. }
  204. }
  205. exports.default = NotifyReporter;
  206. _defineProperty(NotifyReporter, 'filename', __filename);
  207. function loadNotifier() {
  208. try {
  209. return require('node-notifier');
  210. } catch (err) {
  211. if (err.code !== 'MODULE_NOT_FOUND') {
  212. throw err;
  213. }
  214. throw Error(
  215. 'notify reporter requires optional peer dependency "node-notifier" but it was not found'
  216. );
  217. }
  218. }