-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Changes from all commits
ced14ea
7bea23b
3f25876
fc99e6d
32bb3cc
d36f9ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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", | ||
}, | ||
] | ||
`; |
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(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you might be hit by https://github.com/facebook/jest/pull/4639/files#r143555141 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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( | ||
|
@@ -83,5 +103,7 @@ export default class NotifyReporter extends BaseReporter { | |
}, | ||
); | ||
} | ||
this._context.previousSuccess = success; | ||
this._context.firstRun = false; | ||
} | ||
} |
There was a problem hiding this comment.
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
andonFailure
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 allownotifyMode
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
There was a problem hiding this comment.
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 withfailure-change
success-change
would accomplish the inverse.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, those would be the same.
failure
,success
,always
,change
,failure-change
andsuccess-change
works for meThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just added those :)