Skip to content

Created --notifyMode option for notifications on certain events #5125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's missing the mode I would use, which is "on every failure, and change to success". I want a notification that my tests are still red on every run until they're green, and when it turns green, but not on every run when it's green. I'm not sure how to best express it, though. Travis has onSuccess and onFailure with different modes, but it feels a bit overkill to add two new options.

What about: always, failure, success, change, failure-always, failure-change, success-always, success-change, and allow notifyMode to be passed an array instead of a single string?

I'm not sure if that's the best option, it's just what I could come up with without thinking too much about it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the difference between failure-always and failure (same with success and success-always)? I personally would prefer keeping it a string because I can only imagine 2 cases from the current combination of options. e.g. ['failure', 'change'] to accomplish the case you mentioned, and I think it would be better expressed as a string with failure-change

success-change would accomplish the inverse.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the difference between failure-always and failure

Good point, those would be the same. failure, success, always, change, failure-change and success-change works for me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added those :)

* `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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what else I'm missing but when I log globalConfig I see that notify is set to true but notifyMode is undefined. However when I go into https://github.com/facebook/jest/blob/v22.0.0/packages/jest-cli/src/cli/index.js#L72
I see that notifyMode is set to the value I pass in.

Copy link
Member

@SimenB SimenB Dec 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was it 🥇 still have to work on the change option though. It seems like a new instance of NotifyReporter is created every time jest --watch reruns. I'll look into that.

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