forked from jestjs/jest
/
NotifyReporter.ts
156 lines (135 loc) Β· 4.59 KB
/
NotifyReporter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as path from 'path';
import * as util from 'util';
import exit = require('exit');
import type {AggregatedResult} from '@jest/test-result';
import type {Config} from '@jest/types';
import {pluralize} from 'jest-util';
import BaseReporter from './BaseReporter';
import type {Context, TestSchedulerContext} from './types';
const isDarwin = process.platform === 'darwin';
const icon = path.resolve(__dirname, '../assets/jest_logo.png');
export default class NotifyReporter extends BaseReporter {
private _notifier = loadNotifier();
private _startRun: (globalConfig: Config.GlobalConfig) => unknown;
private _globalConfig: Config.GlobalConfig;
private _context: TestSchedulerContext;
static readonly filename = __filename;
constructor(
globalConfig: Config.GlobalConfig,
startRun: (globalConfig: Config.GlobalConfig) => unknown,
context: TestSchedulerContext,
) {
super();
this._globalConfig = globalConfig;
this._startRun = startRun;
this._context = context;
}
onRunComplete(contexts: Set<Context>, result: AggregatedResult): void {
const success =
result.numFailedTests === 0 && result.numRuntimeErrorTestSuites === 0;
const firstContext = contexts.values().next();
const hasteFS =
firstContext && firstContext.value && firstContext.value.hasteFS;
let packageName;
if (hasteFS != null) {
// assuming root package.json is the first one
const [filePath] = hasteFS.matchFiles('package.json');
packageName =
filePath != null
? hasteFS.getModuleName(filePath)
: this._globalConfig.rootDir;
} else {
packageName = this._globalConfig.rootDir;
}
packageName = packageName != null ? `${packageName} - ` : '';
const notifyMode = this._globalConfig.notifyMode;
const statusChanged =
this._context.previousSuccess !== success || this._context.firstRun;
const testsHaveRun = result.numTotalTests !== 0;
if (
testsHaveRun &&
success &&
(notifyMode === 'always' ||
notifyMode === 'success' ||
notifyMode === 'success-change' ||
(notifyMode === 'change' && statusChanged) ||
(notifyMode === 'failure-change' && statusChanged))
) {
const title = util.format('%s%d%% Passed', packageName, 100);
const message = `${isDarwin ? '\u2705 ' : ''}${pluralize(
'test',
result.numPassedTests,
)} passed`;
this._notifier.notify({icon, message, timeout: false, title});
} else if (
testsHaveRun &&
!success &&
(notifyMode === 'always' ||
notifyMode === 'failure' ||
notifyMode === 'failure-change' ||
(notifyMode === 'change' && statusChanged) ||
(notifyMode === 'success-change' && statusChanged))
) {
const failed = result.numFailedTests / result.numTotalTests;
const title = util.format(
'%s%d%% Failed',
packageName,
Math.ceil(Number.isNaN(failed) ? 0 : failed * 100),
);
const message = util.format(
(isDarwin ? '\u26D4\uFE0F ' : '') + '%d of %d tests failed',
result.numFailedTests,
result.numTotalTests,
);
const watchMode = this._globalConfig.watch || this._globalConfig.watchAll;
const restartAnswer = 'Run again';
const quitAnswer = 'Exit tests';
if (!watchMode) {
this._notifier.notify({icon, message, timeout: false, title});
} else {
this._notifier.notify(
{
actions: [restartAnswer, quitAnswer],
closeLabel: 'Close',
icon,
message,
timeout: false,
title,
},
(err, _, metadata) => {
if (err || !metadata) {
return;
}
if (metadata.activationValue === quitAnswer) {
exit(0);
return;
}
if (metadata.activationValue === restartAnswer) {
this._startRun(this._globalConfig);
}
},
);
}
}
this._context.previousSuccess = success;
this._context.firstRun = false;
}
}
function loadNotifier(): typeof import('node-notifier') {
try {
return require('node-notifier');
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err;
}
throw Error(
'notify reporter requires optional peer dependency "node-notifier" but it was not found',
);
}
}