From 033aed3f219fba0846dc305aa0a8b28651b0d351 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Caleb=20=E3=83=84=20Everett?= Date: Tue, 3 Sep 2019 15:18:05 -0700 Subject: [PATCH] feat: make node-notifier an optional dependency --- CHANGELOG.md | 1 + packages/jest-reporters/package.json | 4 +- .../src/__tests__/notify_impl.test.ts | 43 ++++++++++++++++++ packages/jest-reporters/src/notify_impl.ts | 44 +++++++++++++++++++ .../jest-reporters/src/notify_reporter.ts | 2 +- 5 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 packages/jest-reporters/src/__tests__/notify_impl.test.ts create mode 100644 packages/jest-reporters/src/notify_impl.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f8139630c88..25c53e121ac4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - `[jest-snapshot]` Remove only the added newlines in multiline snapshots ([#8859](https://github.com/facebook/jest/pull/8859)) - `[jest-snapshot]` Distinguish empty string from external snapshot not written ([#8880](https://github.com/facebook/jest/pull/8880)) - `[jest-snapshot]` [**BREAKING**] Distinguish empty string from internal snapshot not written ([#8898](https://github.com/facebook/jest/pull/8898)) +- `[jest-reporters]` Make node-notifer an optional dependency ([#8918](https://github.com/facebook/jest/pull/8918)) ### Chore & Maintenance diff --git a/packages/jest-reporters/package.json b/packages/jest-reporters/package.json index d6f3cb2f3eb8..26e2478139ab 100644 --- a/packages/jest-reporters/package.json +++ b/packages/jest-reporters/package.json @@ -23,7 +23,6 @@ "jest-runtime": "^24.9.0", "jest-util": "^24.9.0", "jest-worker": "^24.6.0", - "node-notifier": "^5.4.3", "slash": "^3.0.0", "source-map": "^0.6.0", "string-length": "^3.1.0" @@ -39,6 +38,9 @@ "@types/node-notifier": "^5.4.0", "strip-ansi": "^5.0.0" }, + "optionalDependencies": { + "node-notifier": "^5.4.3" + }, "engines": { "node": ">= 8" }, diff --git a/packages/jest-reporters/src/__tests__/notify_impl.test.ts b/packages/jest-reporters/src/__tests__/notify_impl.test.ts new file mode 100644 index 000000000000..954054367b4d --- /dev/null +++ b/packages/jest-reporters/src/__tests__/notify_impl.test.ts @@ -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); +}); diff --git a/packages/jest-reporters/src/notify_impl.ts b/packages/jest-reporters/src/notify_impl.ts new file mode 100644 index 000000000000..149cbf63cdeb --- /dev/null +++ b/packages/jest-reporters/src/notify_impl.ts @@ -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; + timeout?: number; +} + +interface NotificationCallback { + (err: Error | null, response: string, metadata?: NotificationMetadata): void; +} + +interface NotificationMetadata { + activationValue?: string; +} diff --git a/packages/jest-reporters/src/notify_reporter.ts b/packages/jest-reporters/src/notify_reporter.ts index 7735baaf2d81..b25edbd63e88 100644 --- a/packages/jest-reporters/src/notify_reporter.ts +++ b/packages/jest-reporters/src/notify_reporter.ts @@ -10,9 +10,9 @@ import * as util from 'util'; import exit = require('exit'); import {Config} from '@jest/types'; import {AggregatedResult} from '@jest/test-result'; -import {notify} from 'node-notifier'; import {Context, TestSchedulerContext} from './types'; import BaseReporter from './base_reporter'; +import notify from './notify_impl'; const isDarwin = process.platform === 'darwin';