@@ -2,6 +2,10 @@ import { BasicLogger, LDLogLevel } from '../../src';
22
33const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
44
5+ beforeEach ( ( ) => {
6+ jest . clearAllMocks ( ) ;
7+ } ) ;
8+
59describe . each < [ LDLogLevel , string [ ] ] > ( [
610 [
711 'debug' ,
@@ -64,10 +68,6 @@ describe('given a logger with a custom name', () => {
6468describe ( 'given a default logger' , ( ) => {
6569 const logger = new BasicLogger ( { } ) ;
6670
67- beforeEach ( ( ) => {
68- jest . clearAllMocks ( ) ;
69- } ) ;
70-
7171 it ( 'logs to the console' , ( ) => {
7272 logger . warn ( 'potato' , 'bacon' ) ;
7373 expect ( spy ) . toHaveBeenCalledWith ( 'potato' , 'bacon' ) ;
@@ -81,10 +81,6 @@ describe('given a logger with a destination that throws', () => {
8181 } ,
8282 } ) ;
8383
84- beforeEach ( ( ) => {
85- jest . clearAllMocks ( ) ;
86- } ) ;
87-
8884 it ( 'logs to the console instead of throwing' , ( ) => {
8985 logger . error ( 'a' ) ;
9086 expect ( spy ) . toHaveBeenCalledWith ( 'error: [LaunchDarkly] a' ) ;
@@ -94,10 +90,6 @@ describe('given a logger with a destination that throws', () => {
9490describe ( 'given a logger with a formatter that throws' , ( ) => {
9591 const strings : string [ ] = [ ] ;
9692
97- beforeEach ( ( ) => {
98- jest . clearAllMocks ( ) ;
99- } ) ;
100-
10193 const logger = new BasicLogger ( {
10294 destination : ( ...args : any ) => {
10395 strings . push ( args . join ( ' ' ) ) ;
@@ -112,3 +104,102 @@ describe('given a logger with a formatter that throws', () => {
112104 expect ( spy ) . toHaveBeenCalledTimes ( 0 ) ;
113105 } ) ;
114106} ) ;
107+
108+ it ( 'dispatches logs correctly with multiple destinations' , ( ) => {
109+ const debug = jest . fn ( ) ;
110+ const info = jest . fn ( ) ;
111+ const warn = jest . fn ( ) ;
112+ const error = jest . fn ( ) ;
113+
114+ const logger = new BasicLogger ( {
115+ destination : {
116+ debug,
117+ info,
118+ warn,
119+ error,
120+ } ,
121+ level : 'debug' ,
122+ } ) ;
123+
124+ logger . debug ( 'toDebug' ) ;
125+ logger . info ( 'toInfo' ) ;
126+ logger . warn ( 'toWarn' ) ;
127+ logger . error ( 'toError' ) ;
128+
129+ expect ( debug ) . toHaveBeenCalledTimes ( 1 ) ;
130+ expect ( debug ) . toHaveBeenCalledWith ( 'debug: [LaunchDarkly] toDebug' ) ;
131+
132+ expect ( info ) . toHaveBeenCalledTimes ( 1 ) ;
133+ expect ( info ) . toHaveBeenCalledWith ( 'info: [LaunchDarkly] toInfo' ) ;
134+
135+ expect ( warn ) . toHaveBeenCalledTimes ( 1 ) ;
136+ expect ( warn ) . toHaveBeenCalledWith ( 'warn: [LaunchDarkly] toWarn' ) ;
137+
138+ expect ( error ) . toHaveBeenCalledTimes ( 1 ) ;
139+ expect ( error ) . toHaveBeenCalledWith ( 'error: [LaunchDarkly] toError' ) ;
140+ } ) ;
141+
142+ it ( 'handles destinations which throw' , ( ) => {
143+ const debug = jest . fn ( ( ) => {
144+ throw new Error ( 'bad' ) ;
145+ } ) ;
146+ const info = jest . fn ( ( ) => {
147+ throw new Error ( 'bad' ) ;
148+ } ) ;
149+ const warn = jest . fn ( ( ) => {
150+ throw new Error ( 'bad' ) ;
151+ } ) ;
152+ const error = jest . fn ( ( ) => {
153+ throw new Error ( 'bad' ) ;
154+ } ) ;
155+
156+ const logger = new BasicLogger ( {
157+ destination : {
158+ debug,
159+ info,
160+ warn,
161+ error,
162+ } ,
163+ level : 'debug' ,
164+ } ) ;
165+
166+ logger . debug ( 'toDebug' ) ;
167+ logger . info ( 'toInfo' ) ;
168+ logger . warn ( 'toWarn' ) ;
169+ logger . error ( 'toError' ) ;
170+
171+ expect ( spy ) . toHaveBeenCalledTimes ( 4 ) ;
172+ expect ( spy ) . toHaveBeenCalledWith ( 'debug: [LaunchDarkly] toDebug' ) ;
173+ expect ( spy ) . toHaveBeenCalledWith ( 'info: [LaunchDarkly] toInfo' ) ;
174+ expect ( spy ) . toHaveBeenCalledWith ( 'warn: [LaunchDarkly] toWarn' ) ;
175+ expect ( spy ) . toHaveBeenCalledWith ( 'error: [LaunchDarkly] toError' ) ;
176+ } ) ;
177+
178+ it ( 'handles destinations which are not defined' , ( ) => {
179+ const debug = jest . fn ( ) ;
180+ const info = jest . fn ( ) ;
181+ const logger = new BasicLogger ( {
182+ // @ts -ignore
183+ destination : {
184+ debug,
185+ info,
186+ } ,
187+ level : 'debug' ,
188+ } ) ;
189+
190+ logger . debug ( 'toDebug' ) ;
191+ logger . info ( 'toInfo' ) ;
192+ logger . warn ( 'toWarn' ) ;
193+ logger . error ( 'toError' ) ;
194+
195+ expect ( debug ) . toHaveBeenCalledTimes ( 1 ) ;
196+ expect ( debug ) . toHaveBeenCalledWith ( 'debug: [LaunchDarkly] toDebug' ) ;
197+
198+ expect ( info ) . toHaveBeenCalledTimes ( 1 ) ;
199+ expect ( info ) . toHaveBeenCalledWith ( 'info: [LaunchDarkly] toInfo' ) ;
200+
201+ expect ( spy ) . toHaveBeenCalledTimes ( 2 ) ;
202+
203+ expect ( spy ) . toHaveBeenCalledWith ( 'toWarn' ) ;
204+ expect ( spy ) . toHaveBeenCalledWith ( 'toError' ) ;
205+ } ) ;
0 commit comments