@@ -17,6 +17,15 @@ const mockHub = {
1717 captureException : jest . fn ( ) ,
1818} ;
1919
20+ const mockConsole = {
21+ debug : jest . fn ( ) ,
22+ log : jest . fn ( ) ,
23+ warn : jest . fn ( ) ,
24+ error : jest . fn ( ) ,
25+ assert : jest . fn ( ) ,
26+ info : jest . fn ( ) ,
27+ } ;
28+
2029const getMockHubWithIntegration = ( integration : Integration ) =>
2130 ( {
2231 ...mockHub ,
@@ -27,63 +36,79 @@ const getMockHubWithIntegration = (integration: Integration) =>
2736const originalConsole = Object . assign ( { } , global . console ) ;
2837
2938describe ( 'CaptureConsole setup' , ( ) => {
39+ beforeEach ( ( ) => {
40+ // this suppresses output to the terminal running the tests, but doesn't interfere with our wrapping
41+ Object . assign ( global . console , mockConsole ) ;
42+ } ) ;
43+
3044 afterEach ( ( ) => {
3145 jest . clearAllMocks ( ) ;
3246
3347 // Un-monkey-patch the console functions
3448 Object . assign ( global . console , originalConsole ) ;
3549 } ) ;
3650
37- it ( 'should patch user-configured console levels ', ( ) => {
38- const captureConsoleIntegration = new CaptureConsole ( { levels : [ 'log' , 'warn' ] } ) ;
39- captureConsoleIntegration . setupOnce (
40- ( ) => undefined ,
41- ( ) => getMockHubWithIntegration ( captureConsoleIntegration ) ,
42- ) ;
51+ describe ( 'monkeypatching ', ( ) => {
52+ beforeEach ( ( ) => {
53+ // for these tests only, we don't want to use the mock console, because we're testing for equality to methods from
54+ // the original, so undo the global `beforeEach()`
55+ Object . assign ( global . console , originalConsole ) ;
56+ } ) ;
4357
44- expect ( global . console . error ) . toBe ( originalConsole . error ) ; // not monkey patched
45- expect ( global . console . log ) . not . toBe ( originalConsole . log ) ; // monkey patched
46- expect ( global . console . warn ) . not . toBe ( originalConsole . warn ) ; // monkey patched
47- } ) ;
58+ it ( 'should patch user-configured console levels' , ( ) => {
59+ const captureConsoleIntegration = new CaptureConsole ( { levels : [ 'log' , 'warn' ] } ) ;
60+ captureConsoleIntegration . setupOnce (
61+ ( ) => undefined ,
62+ ( ) => getMockHubWithIntegration ( captureConsoleIntegration ) ,
63+ ) ;
4864
49- it ( 'should fall back to default console levels if none are provided' , ( ) => {
50- const captureConsoleIntegration = new CaptureConsole ( ) ;
51- captureConsoleIntegration . setupOnce (
52- ( ) => undefined ,
53- ( ) => getMockHubWithIntegration ( captureConsoleIntegration ) ,
54- ) ;
65+ expect ( global . console . error ) . toBe ( originalConsole . error ) ; // not monkey patched
66+ expect ( global . console . log ) . not . toBe ( originalConsole . log ) ; // monkey patched
67+ expect ( global . console . warn ) . not . toBe ( originalConsole . warn ) ; // monkey patched
68+ } ) ;
5569
56- // expect a set of defined console levels to have been monkey patched
57- expect ( global . console . debug ) . not . toBe ( originalConsole . debug ) ;
58- expect ( global . console . info ) . not . toBe ( originalConsole . info ) ;
59- expect ( global . console . warn ) . not . toBe ( originalConsole . warn ) ;
60- expect ( global . console . error ) . not . toBe ( originalConsole . error ) ;
61- expect ( global . console . log ) . not . toBe ( originalConsole . log ) ;
62- expect ( global . console . assert ) . not . toBe ( originalConsole . assert ) ;
70+ it ( 'should fall back to default console levels if none are provided' , ( ) => {
71+ const captureConsoleIntegration = new CaptureConsole ( ) ;
72+ captureConsoleIntegration . setupOnce (
73+ ( ) => undefined ,
74+ ( ) => getMockHubWithIntegration ( captureConsoleIntegration ) ,
75+ ) ;
6376
64- // any other fields should not have been patched
65- expect ( global . console . trace ) . toBe ( originalConsole . trace ) ;
66- expect ( global . console . table ) . toBe ( originalConsole . table ) ;
67- } ) ;
77+ // expect a set of defined console levels to have been monkey patched
78+ expect ( global . console . debug ) . not . toBe ( originalConsole . debug ) ;
79+ expect ( global . console . info ) . not . toBe ( originalConsole . info ) ;
80+ expect ( global . console . warn ) . not . toBe ( originalConsole . warn ) ;
81+ expect ( global . console . error ) . not . toBe ( originalConsole . error ) ;
82+ expect ( global . console . log ) . not . toBe ( originalConsole . log ) ;
83+ expect ( global . console . assert ) . not . toBe ( originalConsole . assert ) ;
84+
85+ // any other fields should not have been patched
86+ expect ( global . console . trace ) . toBe ( originalConsole . trace ) ;
87+ expect ( global . console . table ) . toBe ( originalConsole . table ) ;
88+ } ) ;
89+
90+ it ( 'should not wrap any functions with an empty levels option' , ( ) => {
91+ const captureConsoleIntegration = new CaptureConsole ( { levels : [ ] } ) ;
92+ captureConsoleIntegration . setupOnce (
93+ ( ) => undefined ,
94+ ( ) => getMockHubWithIntegration ( captureConsoleIntegration ) ,
95+ ) ;
6896
69- it ( 'should not wrap any functions with an empty levels option' , ( ) => {
70- const captureConsoleIntegration = new CaptureConsole ( { levels : [ ] } ) ;
71- captureConsoleIntegration . setupOnce (
72- ( ) => undefined ,
73- ( ) => getMockHubWithIntegration ( captureConsoleIntegration ) ,
74- ) ;
97+ // expect the default set of console levels not to have been monkey patched
98+ expect ( global . console . debug ) . toBe ( originalConsole . debug ) ;
99+ expect ( global . console . info ) . toBe ( originalConsole . info ) ;
100+ expect ( global . console . warn ) . toBe ( originalConsole . warn ) ;
101+ expect ( global . console . error ) . toBe ( originalConsole . error ) ;
102+ expect ( global . console . log ) . toBe ( originalConsole . log ) ;
103+ expect ( global . console . assert ) . toBe ( originalConsole . assert ) ;
75104
76- // expect the default set of console levels not to have been monkey patched
77- expect ( global . console . debug ) . toBe ( originalConsole . debug ) ;
78- expect ( global . console . info ) . toBe ( originalConsole . info ) ;
79- expect ( global . console . warn ) . toBe ( originalConsole . warn ) ;
80- expect ( global . console . error ) . toBe ( originalConsole . error ) ;
81- expect ( global . console . log ) . toBe ( originalConsole . log ) ;
82- expect ( global . console . assert ) . toBe ( originalConsole . assert ) ;
105+ // suppress output from the logging we're about to do
106+ global . console . log = global . console . info = jest . fn ( ) ;
83107
84- // expect no message to be captured with console.log
85- global . console . log ( 'some message' ) ;
86- expect ( mockHub . captureMessage ) . not . toHaveBeenCalled ( ) ;
108+ // expect no message to be captured with console.log
109+ global . console . log ( 'some message' ) ;
110+ expect ( mockHub . captureMessage ) . not . toHaveBeenCalled ( ) ;
111+ } ) ;
87112 } ) ;
88113
89114 it ( 'setup should fail gracefully when console is not available' , ( ) => {
0 commit comments