Skip to content

Commit

Permalink
added notifyMode flag to specify when a notification should display
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
psilospore committed Feb 5, 2018
1 parent a24204a commit 42cd78c
Show file tree
Hide file tree
Showing 17 changed files with 350 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@

### Features

* `[jest-cli]` Added --notifyMode to specify when to be notified.
([#5125](https://github.com/facebook/jest/pull/5125))
* `[jest-message-util]` Add codeframe to test assertion failures
([#5087](https://github.com/facebook/jest/pull/5087))
* `[jest-config]` Add Global Setup/Teardown options
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,141 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

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

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

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

exports[`test success-change 1`] = `
Array [
Object {
"icon": true,
"message": "✅ 3 tests passed",
"title": "100% Passed",
},
Object {
"icon": true,
"message": "⛔️ 3 of 3 tests failed",
"title": "100% Failed",
},
Object {
"icon": true,
"message": "✅ 3 tests passed",
"title": "100% Passed",
},
Object {
"icon": true,
"message": "✅ 3 tests passed",
"title": "100% Passed",
},
Object {
"icon": true,
"message": "⛔️ 3 of 3 tests failed",
"title": "100% Failed",
},
]
`;
122 changes: 122 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,122 @@
/**
* 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';
import type {Jest as jest} from '../../../../types/Jest';

const ICON_PATH = '/assets/jest_logo.png';

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,
];

const iconShown = path => path.endsWith(ICON_PATH);

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');
notify.notify.mock.calls = [];

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(([{icon, message, title}]) => ({
icon: iconShown(icon),
message,
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);
});
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 @@ -365,6 +365,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;
}
}
Loading

0 comments on commit 42cd78c

Please sign in to comment.