Skip to content

Commit

Permalink
Created --notifyMode option for notifications on certain events (#5125)
Browse files Browse the repository at this point in the history
* added notifyMode flag to specify when a notification should display

forgot about other notifyMode configs

add notifyMode to normalize

Created TestSchedulerContext to save previous status of test to make the change option work

updated docs

minor linting fix

Added additional options such as success-change and failure-change

Put conditions back in if else clauses

Fixed documentation on notifyMode

Added notify reporter test (for review)

Finished NotifyReporter tests. Testing against simulated sequences of events.

* hipsters and their emojis 😞

* icons might not show in some envs

* Update CHANGELOG.md
  • Loading branch information
psilospore authored and cpojer committed Feb 7, 2018
1 parent ad91d0a commit 1947496
Show file tree
Hide file tree
Showing 17 changed files with 324 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Features

* `[jest-cli]` Added `--notifyMode` to specify when to be notified.
([#5125](https://github.com/facebook/jest/pull/5125))
* `[diff-sequences]` New package compares items in two sequences to find a
**longest common subsequence**.
([#5407](https://github.com/facebook/jest/pull/5407))
Expand Down
15 changes: 15 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,21 @@ Default: `false`

Activates notifications for test results.

### `notifyMode` [string]

Default: `always`

Specifies notification mode. Requires `notify: true`.

#### Modes

* `always`: always send a notification.
* `failure`: send a notification when tests fail.
* `success`: send a notification when tests pass.
* `change`: send a notification when the status changed.
* `success-change`: send a notification when tests pass or once when it fails.
* `failure-success`: send a notification when tests fails or once when it passes.

### `preset` [string]

Default: `undefined`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
\\"noStackTrace\\": false,
\\"nonFlagArgs\\": [],
\\"notify\\": false,
\\"notifyMode\\": \\"always\\",
\\"passWithNoTests\\": false,
\\"rootDir\\": \\"<<REPLACED_ROOT_DIR>>\\",
\\"runTestsByPath\\": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`test always 1`] = `
Array [
Object {
"message": "3 tests passed",
"title": "100% Passed",
},
Object {
"message": "3 of 3 tests failed",
"title": "100% Failed",
},
Object {
"message": "3 tests passed",
"title": "100% Passed",
},
Object {
"message": "3 tests passed",
"title": "100% Passed",
},
Object {
"message": "3 of 3 tests failed",
"title": "100% Failed",
},
Object {
"message": "3 of 3 tests failed",
"title": "100% Failed",
},
]
`;

exports[`test change 1`] = `
Array [
Object {
"message": "3 tests passed",
"title": "100% Passed",
},
Object {
"message": "3 of 3 tests failed",
"title": "100% Failed",
},
Object {
"message": "3 tests passed",
"title": "100% Passed",
},
Object {
"message": "3 of 3 tests failed",
"title": "100% Failed",
},
]
`;

exports[`test failure-change 1`] = `
Array [
Object {
"message": "3 tests passed",
"title": "100% Passed",
},
Object {
"message": "3 of 3 tests failed",
"title": "100% Failed",
},
Object {
"message": "3 tests passed",
"title": "100% Passed",
},
Object {
"message": "3 of 3 tests failed",
"title": "100% Failed",
},
Object {
"message": "3 of 3 tests failed",
"title": "100% Failed",
},
]
`;

exports[`test success 1`] = `
Array [
Object {
"message": "3 tests passed",
"title": "100% Passed",
},
Object {
"message": "3 tests passed",
"title": "100% Passed",
},
Object {
"message": "3 tests passed",
"title": "100% Passed",
},
]
`;

exports[`test success-change 1`] = `
Array [
Object {
"message": "3 tests passed",
"title": "100% Passed",
},
Object {
"message": "3 of 3 tests failed",
"title": "100% Failed",
},
Object {
"message": "3 tests passed",
"title": "100% Passed",
},
Object {
"message": "3 tests passed",
"title": "100% Passed",
},
Object {
"message": "3 of 3 tests failed",
"title": "100% Failed",
},
]
`;
119 changes: 119 additions & 0 deletions packages/jest-cli/src/__tests__/notify_reporter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* 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.
*
*/

'use strict';

import TestScheduler from '../test_scheduler';
import NotifyReporter from '../reporters/notify_reporter';
import type {TestSchedulerContext} from '../test_scheduler';
import type {AggregatedResult} from '../../../../types/TestResult';

jest.mock('../reporters/default_reporter');
jest.mock('node-notifier', () => ({
notify: jest.fn(),
}));

const initialContext: TestSchedulerContext = {
firstRun: true,
previousSuccess: false,
};

const aggregatedResultsSuccess: AggregatedResult = {
numFailedTestSuites: 0,
numFailedTests: 0,
numPassedTestSuites: 1,
numPassedTests: 3,
numRuntimeErrorTestSuites: 0,
numTotalTestSuites: 1,
numTotalTests: 3,
success: true,
};

const aggregatedResultsFailure: AggregatedResult = {
numFailedTestSuites: 1,
numFailedTests: 3,
numPassedTestSuites: 0,
numPassedTests: 9,
numRuntimeErrorTestSuites: 0,
numTotalTestSuites: 1,
numTotalTests: 3,
success: false,
};

// Simulated sequence of events for NotifyReporter
const notifyEvents = [
aggregatedResultsSuccess,
aggregatedResultsFailure,
aggregatedResultsSuccess,
aggregatedResultsSuccess,
aggregatedResultsFailure,
aggregatedResultsFailure,
];

test('.addReporter() .removeReporter()', () => {
const scheduler = new TestScheduler(
{},
{},
Object.assign({}, initialContext),
);
const reporter = new NotifyReporter();
scheduler.addReporter(reporter);
expect(scheduler._dispatcher._reporters).toContain(reporter);
scheduler.removeReporter(NotifyReporter);
expect(scheduler._dispatcher._reporters).not.toContain(reporter);
});

const testModes = (notifyMode: string, arl: Array<AggregatedResult>) => {
const notify = require('node-notifier');

let previousContext = initialContext;
arl.forEach((ar, i) => {
const newContext = Object.assign(previousContext, {
firstRun: i === 0,
previousSuccess: previousContext.previousSuccess,
});
const reporter = new NotifyReporter(
{notify: true, notifyMode},
{},
newContext,
);
previousContext = newContext;
reporter.onRunComplete(new Set(), ar);
});

expect(
notify.notify.mock.calls.map(([{message, title}]) => ({
message: message.replace('\u26D4\uFE0F ', '').replace('\u2705 ', ''),
title,
})),
).toMatchSnapshot();
};

test('test always', () => {
testModes('always', notifyEvents);
});

test('test success', () => {
testModes('success', notifyEvents);
});

test('test change', () => {
testModes('change', notifyEvents);
});

test('test success-change', () => {
testModes('success-change', notifyEvents);
});

test('test failure-change', () => {
testModes('failure-change', notifyEvents);
});

afterEach(() => {
jest.clearAllMocks();
});
5 changes: 5 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ export const options = {
description: 'Activates notifications for test results.',
type: 'boolean',
},
notifyMode: {
default: 'always',
description: 'Specifies when notifications will appear for test results.',
type: 'string',
},
onlyChanged: {
alias: 'o',
default: undefined,
Expand Down
28 changes: 25 additions & 3 deletions packages/jest-cli/src/reporters/notify_reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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';

Expand All @@ -24,29 +25,48 @@ 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;

if (success) {
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 {
} 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(
Expand Down Expand Up @@ -83,5 +103,7 @@ export default class NotifyReporter extends BaseReporter {
},
);
}
this._context.previousSuccess = success;
this._context.firstRun = false;
}
}
15 changes: 12 additions & 3 deletions packages/jest-cli/src/run_jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ const processResults = (runResults, options) => {
return options.onComplete && options.onComplete(runResults);
};

const testSchedulerContext = {
firstRun: true,
previousSuccess: true,
};

export default (async function runJest({
contexts,
globalConfig,
Expand Down Expand Up @@ -199,9 +204,13 @@ export default (async function runJest({
// $FlowFixMe
await require(globalConfig.globalSetup)();
}
const results = await new TestScheduler(globalConfig, {
startRun,
}).scheduleTests(allTests, testWatcher);
const results = await new TestScheduler(
globalConfig,
{
startRun,
},
testSchedulerContext,
).scheduleTests(allTests, testWatcher);

sequencer.cacheResults(allTests, results);

Expand Down
Loading

0 comments on commit 1947496

Please sign in to comment.