UNPKG

6.53 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
96/**
97 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
98 *
99 * This source code is licensed under the MIT license found in the
100 * LICENSE file in the root directory of this source tree.
101 */
102const isDarwin = process.platform === 'darwin';
103const icon = path().resolve(__dirname, '../assets/jest_logo.png');
104
105class NotifyReporter extends _BaseReporter.default {
106 _notifier = loadNotifier();
107 _globalConfig;
108 _context;
109 static filename = __filename;
110
111 constructor(globalConfig, context) {
112 super();
113 this._globalConfig = globalConfig;
114 this._context = context;
115 }
116
117 onRunComplete(testContexts, result) {
118 const success =
119 result.numFailedTests === 0 && result.numRuntimeErrorTestSuites === 0;
120 const firstContext = testContexts.values().next();
121 const hasteFS =
122 firstContext && firstContext.value && firstContext.value.hasteFS;
123 let packageName;
124
125 if (hasteFS != null) {
126 // assuming root package.json is the first one
127 const [filePath] = hasteFS.matchFiles('package.json');
128 packageName =
129 filePath != null
130 ? hasteFS.getModuleName(filePath)
131 : this._globalConfig.rootDir;
132 } else {
133 packageName = this._globalConfig.rootDir;
134 }
135
136 packageName = packageName != null ? `${packageName} - ` : '';
137 const notifyMode = this._globalConfig.notifyMode;
138 const statusChanged =
139 this._context.previousSuccess !== success || this._context.firstRun;
140 const testsHaveRun = result.numTotalTests !== 0;
141
142 if (
143 testsHaveRun &&
144 success &&
145 (notifyMode === 'always' ||
146 notifyMode === 'success' ||
147 notifyMode === 'success-change' ||
148 (notifyMode === 'change' && statusChanged) ||
149 (notifyMode === 'failure-change' && statusChanged))
150 ) {
151 const title = util().format('%s%d%% Passed', packageName, 100);
152 const message = `${isDarwin ? '\u2705 ' : ''}${(0, _jestUtil().pluralize)(
153 'test',
154 result.numPassedTests
155 )} passed`;
156
157 this._notifier.notify({
158 hint: 'int:transient:1',
159 icon,
160 message,
161 timeout: false,
162 title
163 });
164 } else if (
165 testsHaveRun &&
166 !success &&
167 (notifyMode === 'always' ||
168 notifyMode === 'failure' ||
169 notifyMode === 'failure-change' ||
170 (notifyMode === 'change' && statusChanged) ||
171 (notifyMode === 'success-change' && statusChanged))
172 ) {
173 const failed = result.numFailedTests / result.numTotalTests;
174 const title = util().format(
175 '%s%d%% Failed',
176 packageName,
177 Math.ceil(Number.isNaN(failed) ? 0 : failed * 100)
178 );
179 const message = util().format(
180 `${isDarwin ? '\u26D4\uFE0F ' : ''}%d of %d tests failed`,
181 result.numFailedTests,
182 result.numTotalTests
183 );
184 const watchMode = this._globalConfig.watch || this._globalConfig.watchAll;
185 const restartAnswer = 'Run again';
186 const quitAnswer = 'Exit tests';
187
188 if (!watchMode) {
189 this._notifier.notify({
190 hint: 'int:transient:1',
191 icon,
192 message,
193 timeout: false,
194 title
195 });
196 } else {
197 this._notifier.notify(
198 {
199 // @ts-expect-error - not all options are supported by all systems (specifically `actions` and `hint`)
200 actions: [restartAnswer, quitAnswer],
201 closeLabel: 'Close',
202 hint: 'int:transient:1',
203 icon,
204 message,
205 timeout: false,
206 title
207 },
208 (err, _, metadata) => {
209 if (err || !metadata) {
210 return;
211 }
212
213 if (metadata.activationValue === quitAnswer) {
214 (0, _exit().default)(0);
215 return;
216 }
217
218 if (
219 metadata.activationValue === restartAnswer &&
220 this._context.startRun
221 ) {
222 this._context.startRun(this._globalConfig);
223 }
224 }
225 );
226 }
227 }
228
229 this._context.previousSuccess = success;
230 this._context.firstRun = false;
231 }
232}
233
234exports.default = NotifyReporter;
235
236function loadNotifier() {
237 try {
238 return require('node-notifier');
239 } catch (err) {
240 if (err.code !== 'MODULE_NOT_FOUND') {
241 throw err;
242 }
243
244 throw Error(
245 'notify reporter requires optional peer dependency "node-notifier" but it was not found'
246 );
247 }
248}