forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotify_reporter.js
More file actions
109 lines (99 loc) · 3.2 KB
/
notify_reporter.js
File metadata and controls
109 lines (99 loc) · 3.2 KB
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
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
* @flow
*/
import type {GlobalConfig} from 'types/Config';
import type {AggregatedResult} from 'types/TestResult';
import type {Context} from 'types/Context';
import exit from 'exit';
import path from 'path';
import util from 'util';
import notifier from 'node-notifier';
import BaseReporter from './base_reporter';
import type {TestSchedulerContext} from '../test_scheduler';
const isDarwin = process.platform === 'darwin';
const icon = path.resolve(__dirname, '../assets/jest_logo.png');
export default class NotifyReporter extends BaseReporter {
_startRun: (globalConfig: GlobalConfig) => *;
_globalConfig: GlobalConfig;
_context: TestSchedulerContext;
constructor(
globalConfig: GlobalConfig,
startRun: (globalConfig: GlobalConfig) => *,
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 notifyMode = this._globalConfig.notifyMode;
const statusChanged =
this._context.previousSuccess !== success || this._context.firstRun;
if (
success &&
(notifyMode === 'always' ||
notifyMode === 'success' ||
notifyMode === 'success-change' ||
(notifyMode === 'change' && statusChanged) ||
(notifyMode === 'failure-change' && statusChanged))
) {
const title = util.format('%d%% Passed', 100);
const message = util.format(
(isDarwin ? '\u2705 ' : '') + '%d tests passed',
result.numPassedTests,
);
notifier.notify({icon, message, title});
} else if (
!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(
'%d%% Failed',
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 restartAnswer = 'Run again';
const quitAnswer = 'Exit tests';
notifier.notify(
{
actions: [restartAnswer, quitAnswer],
closeLabel: 'Close',
icon,
message,
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;
}
}