Skip to content

Commit 23a4307

Browse files
committed
added notifyMode flag to specify when a notification should display
1 parent 99c76dd commit 23a4307

File tree

9 files changed

+47
-2
lines changed

9 files changed

+47
-2
lines changed

docs/Configuration.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,18 @@ Default: `false`
384384

385385
Activates notifications for test results.
386386

387+
### `notifyMode` [string]
388+
389+
Default: `always`
390+
391+
Specifies notification mode.
392+
393+
#### Modes
394+
* `always`: always send a notification.
395+
* `failure`: send a notification when tests fail.
396+
* `success`: send a notification when tests pass.
397+
* `change`: send a notification when the status changed.
398+
387399
### `preset` [string]
388400

389401
Default: `undefined`

integration_tests/__tests__/__snapshots__/show_config.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
7878
\\"noStackTrace\\": false,
7979
\\"nonFlagArgs\\": [],
8080
\\"notify\\": false,
81+
\\"notifyMode\\": \\"always\\",
8182
\\"passWithNoTests\\": false,
8283
\\"rootDir\\": \\"<<REPLACED_ROOT_DIR>>\\",
8384
\\"runTestsByPath\\": false,

packages/jest-cli/src/cli/args.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,11 @@ export const options = {
348348
description: 'Activates notifications for test results.',
349349
type: 'boolean',
350350
},
351+
notifyMode: {
352+
default: undefined,
353+
description: 'Specifies when notifications will appear for test results.',
354+
type: 'string',
355+
},
351356
onlyChanged: {
352357
alias: 'o',
353358
default: undefined,

packages/jest-cli/src/reporters/notify_reporter.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const icon = path.resolve(__dirname, '../assets/jest_logo.png');
2323
export default class NotifyReporter extends BaseReporter {
2424
_startRun: (globalConfig: GlobalConfig) => *;
2525
_globalConfig: GlobalConfig;
26+
_previousSuccess: boolean;
2627

2728
constructor(
2829
globalConfig: GlobalConfig,
@@ -31,21 +32,37 @@ export default class NotifyReporter extends BaseReporter {
3132
super();
3233
this._globalConfig = globalConfig;
3334
this._startRun = startRun;
35+
this._previousSuccess = false;
36+
this._firstRun = true;
3437
}
3538

3639
onRunComplete(contexts: Set<Context>, result: AggregatedResult): void {
3740
const success =
3841
result.numFailedTests === 0 && result.numRuntimeErrorTestSuites === 0;
3942

40-
if (success) {
43+
if (
44+
success &&
45+
(this._globalConfig === 'always' ||
46+
this._globalConfig === 'success' ||
47+
(this._globalConfig === 'change' &&
48+
(!this._previousSuccess || this._firstRun)))
49+
) {
4150
const title = util.format('%d%% Passed', 100);
4251
const message = util.format(
4352
(isDarwin ? '\u2705 ' : '') + '%d tests passed',
4453
result.numPassedTests,
4554
);
4655

4756
notifier.notify({icon, message, title});
48-
} else {
57+
this._previousSuccess = true;
58+
this._firstRun = false;
59+
} else if (
60+
!success &&
61+
(this._globalConfig === 'always' ||
62+
this._globalConfig === 'failure' ||
63+
(this._globalConfig === 'change' &&
64+
(this._previousSuccess || this._firstRun)))
65+
) {
4966
const failed = result.numFailedTests / result.numTotalTests;
5067

5168
const title = util.format(
@@ -81,6 +98,8 @@ export default class NotifyReporter extends BaseReporter {
8198
}
8299
},
83100
);
101+
this._previousSuccess = false;
102+
this._firstRun = false;
84103
}
85104
}
86105
}

packages/jest-config/src/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export default ({
5151
modulePathIgnorePatterns: [],
5252
noStackTrace: false,
5353
notify: false,
54+
notifyMode: 'always',
5455
preset: null,
5556
resetMocks: false,
5657
resetModules: false,

packages/jest-config/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const getConfigs = (
9898
noStackTrace: options.noStackTrace,
9999
nonFlagArgs: options.nonFlagArgs,
100100
notify: options.notify,
101+
notifyMode: options.notifyMode,
101102
onlyChanged: options.onlyChanged,
102103
onlyFailures: options.onlyFailures,
103104
outputFile: options.outputFile,

packages/jest-validate/src/__tests__/fixtures/jest_config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const defaultConfig = {
3939
modulePathIgnorePatterns: [],
4040
noStackTrace: false,
4141
notify: false,
42+
notifyMode: 'always',
4243
preset: null,
4344
resetMocks: false,
4445
resetModules: false,
@@ -95,6 +96,7 @@ const validConfig = {
9596
name: 'string',
9697
noStackTrace: false,
9798
notify: false,
99+
notifyMode: 'always',
98100
preset: 'react-native',
99101
resetMocks: false,
100102
resetModules: false,

test_utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const DEFAULT_GLOBAL_CONFIG: GlobalConfig = {
3737
noStackTrace: false,
3838
nonFlagArgs: [],
3939
notify: false,
40+
notifyMode: 'always',
4041
onlyChanged: false,
4142
onlyFailures: false,
4243
outputFile: null,

types/Config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type DefaultOptions = {|
4343
modulePathIgnorePatterns: Array<string>,
4444
noStackTrace: boolean,
4545
notify: boolean,
46+
notifyMode: string,
4647
preset: ?string,
4748
resetMocks: boolean,
4849
resetModules: boolean,
@@ -105,6 +106,7 @@ export type InitialOptions = {
105106
name?: string,
106107
noStackTrace?: boolean,
107108
notify?: boolean,
109+
notifyMode?: string,
108110
onlyChanged?: boolean,
109111
outputFile?: Path,
110112
passWithNoTests?: boolean,
@@ -179,6 +181,7 @@ export type GlobalConfig = {|
179181
nonFlagArgs: Array<string>,
180182
noSCM: ?boolean,
181183
notify: boolean,
184+
notifyMode: string,
182185
outputFile: ?Path,
183186
onlyChanged: boolean,
184187
onlyFailures: boolean,

0 commit comments

Comments
 (0)