From 3730c523ecb78d9bce19a8709ebbcc3bbb312274 Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Tue, 8 Oct 2019 08:43:23 -0700 Subject: [PATCH] Add explicit StubLogger option for tests Summary: A bit ugly to have test-only code in prod logic, but we don't need any of the logger logic in tests and by pulling out the `StubLogger`, we can avoid loading Electron during tests. Reviewed By: jknoxville Differential Revision: D17808479 fbshipit-source-id: d2295d7bbca2f09c3f23389ad9443648757d8d03 --- src/fb-interfaces/Logger.tsx | 5 +++++ src/fb-stubs/Logger.tsx | 30 +++--------------------------- src/utils/StubLogger.tsx | 30 ++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 src/utils/StubLogger.tsx diff --git a/src/fb-interfaces/Logger.tsx b/src/fb-interfaces/Logger.tsx index e1ae853bf..b4249b41b 100644 --- a/src/fb-interfaces/Logger.tsx +++ b/src/fb-interfaces/Logger.tsx @@ -13,6 +13,11 @@ export type TrackType = | 'success-rate' | 'operation-cancelled'; +export type Args = { + isHeadless?: boolean; + isTest?: boolean; +}; + export interface Logger { track(type: TrackType, event: string, data?: any, plugin?: string): void; diff --git a/src/fb-stubs/Logger.tsx b/src/fb-stubs/Logger.tsx index 0096b4179..570403e2f 100644 --- a/src/fb-stubs/Logger.tsx +++ b/src/fb-stubs/Logger.tsx @@ -5,37 +5,13 @@ * @format */ -import {TrackType, Logger} from '../fb-interfaces/Logger'; +import {Logger, Args} from '../fb-interfaces/Logger'; +import StubLogger from '../utils/StubLogger'; import {Store} from '../reducers/index'; -import ScribeLogger from './ScribeLogger'; let instance: StubLogger | null = null; -type Args = { - isHeadless?: boolean; -}; - -class StubLogger implements Logger { - constructor(store: Store, args?: Args) { - this.scribeLogger = new ScribeLogger(this); - } - - scribeLogger: ScribeLogger; - - track(type: TrackType, event: string, data?: any, plugin?: string) {} - - trackTimeSince(mark: string, eventName?: string) {} - - info(data: any, category: string) {} - - warn(data: any, category: string) {} - - error(data: any, category: string) {} - - debug(data: any, category: string) {} -} - -export function init(store: Store, args?: Args): Logger { +export function init(store: Store, _args?: Args): Logger { if (instance) { throw new Error('Attempted to initialize Logger when already initialized'); } diff --git a/src/utils/StubLogger.tsx b/src/utils/StubLogger.tsx new file mode 100644 index 000000000..141d3ac15 --- /dev/null +++ b/src/utils/StubLogger.tsx @@ -0,0 +1,30 @@ +/** + * Copyright 2018-present Facebook. + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * @format + */ + +import {Logger, Args, TrackType} from '../fb-interfaces/Logger'; +import ScribeLogger from '../fb-stubs/ScribeLogger'; +import {Store} from '../reducers/index'; + +export default class StubLogger implements Logger { + constructor(_store: Store, _args?: Args) { + this.scribeLogger = new ScribeLogger(this); + } + + scribeLogger: ScribeLogger; + + track(_type: TrackType, _event: string, _data?: any, _plugin?: string) {} + + trackTimeSince(_mark: string, _eventName?: string) {} + + info(_data: any, _category: string) {} + + warn(_data: any, _category: string) {} + + error(_data: any, _category: string) {} + + debug(_data: any, _category: string) {} +}