-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make node-notifier an optional dependency
- Loading branch information
1 parent
c588c18
commit f59358d
Showing
4 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
test('without node-notifier notify_impl uses mock function that throws an error', () => { | ||
jest.doMock('node-notifier', () => { | ||
const error: any = new Error("Cannot find module 'node-notifier'"); | ||
error.code = 'MODULE_NOT_FOUND'; | ||
throw error; | ||
}); | ||
const notify = require('../notify_impl').default; | ||
const arg = jest.fn(); | ||
expect(() => notify(arg)).toThrow( | ||
'notify reporter requires optional dependeny node-notifier but it was not found', | ||
); | ||
}); | ||
|
||
test('notify_impl throws the error when require throws an unexpected error', () => { | ||
const error = new Error('unexpected require error'); | ||
jest.doMock('node-notifier', () => { | ||
throw error; | ||
}); | ||
expect(() => require('../notify_impl')).toThrow(error); | ||
}); | ||
|
||
test('notify_impl uses node-notifier when it is available', () => { | ||
const mockNodeNotifier = {notify: jest.fn()}; | ||
jest.doMock('node-notifier', () => mockNodeNotifier); | ||
const notify = require('../notify_impl').default; | ||
// notify would be object equal to mockNodeNotifier.notify except we had | ||
// to bind it to mockNodeNotifier, which returns a new function. so | ||
// instead of expect(notify).toBe(mockNodeNotifier.notify) we need to | ||
// check that the arguments and return values are forwarded, and that | ||
// `this` is set to the module. | ||
const arg = jest.fn(); | ||
const result = notify(arg); | ||
expect(mockNodeNotifier.notify).toBeCalledTimes(1); | ||
expect(mockNodeNotifier.notify).toBeCalledWith(arg); | ||
expect(mockNodeNotifier.notify).toReturnWith(result); | ||
expect(mockNodeNotifier.notify.mock.instances[0]).toEqual(mockNodeNotifier); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. 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. | ||
*/ | ||
export default getNotifier(); | ||
|
||
/** | ||
* Return a notifier implementation. | ||
* | ||
* node-notifier is an optional dependency so it may not be installed. | ||
* If node-notifier is not found then a stub notifier is returned that throws | ||
* an error whenever it is used. | ||
*/ | ||
function getNotifier(): Notify { | ||
try { | ||
const notifier: typeof import('node-notifier') = require('node-notifier'); | ||
return notifier.notify.bind(notifier); | ||
} catch (ex) { | ||
if (ex.code !== 'MODULE_NOT_FOUND') { | ||
throw ex; | ||
} | ||
return () => { | ||
throw Error( | ||
'notify reporter requires optional dependeny node-notifier but it was not found', | ||
); | ||
}; | ||
} | ||
} | ||
|
||
export type Notify = ( | ||
notification?: Notification, | ||
callback?: NotificationCallback, | ||
) => unknown; | ||
|
||
interface Notification { | ||
title?: string; | ||
message?: string; | ||
icon?: string; | ||
closeLabel?: string; | ||
actions?: string | Array<string>; | ||
timeout?: number; | ||
} | ||
|
||
interface NotificationCallback { | ||
(err: Error | null, response: string, metadata?: NotificationMetadata): void; | ||
} | ||
|
||
interface NotificationMetadata { | ||
activationValue?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters