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
This commit is contained in:
Pascal Hartig
2019-10-08 08:43:23 -07:00
committed by Facebook Github Bot
parent d86b8caa2d
commit 3730c523ec
3 changed files with 38 additions and 27 deletions

View File

@@ -13,6 +13,11 @@ export type TrackType =
| 'success-rate' | 'success-rate'
| 'operation-cancelled'; | 'operation-cancelled';
export type Args = {
isHeadless?: boolean;
isTest?: boolean;
};
export interface Logger { export interface Logger {
track(type: TrackType, event: string, data?: any, plugin?: string): void; track(type: TrackType, event: string, data?: any, plugin?: string): void;

View File

@@ -5,37 +5,13 @@
* @format * @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 {Store} from '../reducers/index';
import ScribeLogger from './ScribeLogger';
let instance: StubLogger | null = null; let instance: StubLogger | null = null;
type Args = { export function init(store: Store, _args?: Args): Logger {
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 {
if (instance) { if (instance) {
throw new Error('Attempted to initialize Logger when already initialized'); throw new Error('Attempted to initialize Logger when already initialized');
} }

30
src/utils/StubLogger.tsx Normal file
View File

@@ -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) {}
}