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/notify_impl.ts b/packages/jest-reporters/src/notify_impl.ts new file mode 100644 index 000000000000..6550c6078ebb --- /dev/null +++ b/packages/jest-reporters/src/notify_impl.ts @@ -0,0 +1,46 @@ +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; + 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';