-
-
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
Caleb ツ Everett
committed
Sep 9, 2019
1 parent
c588c18
commit 033aed3
Showing
5 changed files
with
92 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
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,43 @@ | ||
/** | ||
* 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. | ||
*/ | ||
import notify from '../notify_impl'; | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
const notification: any = jest.fn(); | ||
const callback: any = jest.fn(); | ||
|
||
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; | ||
}); | ||
expect(() => notify(notification)).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(() => notify(notification)).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; | ||
const result = notify(notification, callback); | ||
expect(mockNodeNotifier.notify).toBeCalledTimes(1); | ||
expect(mockNodeNotifier.notify).toBeCalledWith(notification, callback); | ||
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,44 @@ | ||
/** | ||
* 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 function notify( | ||
notification: Notification, | ||
callback?: NotificationCallback, | ||
) { | ||
try { | ||
return require('node-notifier').notify(notification, callback); | ||
} catch (ex) { | ||
if (ex.code !== 'MODULE_NOT_FOUND') { | ||
throw ex; | ||
} else { | ||
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