UNPKG

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