@@ -16,6 +16,15 @@ const mockHub = {
16
16
captureException : jest . fn ( ) ,
17
17
} ;
18
18
19
+ const mockConsole = {
20
+ debug : jest . fn ( ) ,
21
+ log : jest . fn ( ) ,
22
+ warn : jest . fn ( ) ,
23
+ error : jest . fn ( ) ,
24
+ assert : jest . fn ( ) ,
25
+ info : jest . fn ( ) ,
26
+ } ;
27
+
19
28
const getMockHubWithIntegration = ( integration : Integration ) => ( {
20
29
...mockHub ,
21
30
getIntegration : jest . fn ( ( ) => integration ) ,
@@ -25,63 +34,81 @@ const getMockHubWithIntegration = (integration: Integration) => ({
25
34
const originalConsole = Object . assign ( { } , global . console ) ;
26
35
27
36
describe ( 'CaptureConsole setup' , ( ) => {
37
+ beforeEach ( ( ) => {
38
+ // this suppresses output to the terminal running the tests, but doesn't interfere with our wrapping
39
+ Object . assign ( global . console , mockConsole ) ;
40
+ } ) ;
41
+
28
42
afterEach ( ( ) => {
29
43
jest . clearAllMocks ( ) ;
30
44
31
45
// Un-monkey-patch the console functions
32
46
Object . assign ( global . console , originalConsole ) ;
33
47
} ) ;
34
48
35
- it ( 'should patch user-configured console levels ', ( ) => {
36
- const captureConsoleIntegration = new CaptureConsole ( { levels : [ 'log' , 'warn' ] } ) ;
37
- captureConsoleIntegration . setupOnce (
38
- ( ) => undefined ,
39
- ( ) => getMockHubWithIntegration ( captureConsoleIntegration ) as any ,
40
- ) ;
49
+ describe ( 'monkeypatching ', ( ) => {
50
+ beforeEach ( ( ) => {
51
+ // for these tests only, we don't want to use the mock console, because we're testing for equality to methods from
52
+ // the original, so undo the global `beforeEach()`
53
+ Object . assign ( global . console , originalConsole ) ;
54
+ } ) ;
41
55
42
- expect ( global . console . error ) . toBe ( originalConsole . error ) ; // not monkey patched
43
- expect ( global . console . log ) . not . toBe ( originalConsole . log ) ; // monkey patched
44
- expect ( global . console . warn ) . not . toBe ( originalConsole . warn ) ; // monkey patched
45
- } ) ;
56
+ it ( 'should patch user-configured console levels' , ( ) => {
57
+ global . console . error = originalConsole . error ;
46
58
47
- it ( 'should fall back to default console levels if none are provided' , ( ) => {
48
- const captureConsoleIntegration = new CaptureConsole ( ) ;
49
- captureConsoleIntegration . setupOnce (
50
- ( ) => undefined ,
51
- ( ) => getMockHubWithIntegration ( captureConsoleIntegration ) as any ,
52
- ) ;
59
+ const captureConsoleIntegration = new CaptureConsole ( { levels : [ 'log' , 'warn' ] } ) ;
60
+ captureConsoleIntegration . setupOnce (
61
+ ( ) => undefined ,
62
+ ( ) => getMockHubWithIntegration ( captureConsoleIntegration ) as any ,
63
+ ) ;
53
64
54
- // expect a set of defined console levels to have been monkey patched
55
- expect ( global . console . debug ) . not . toBe ( originalConsole . debug ) ;
56
- expect ( global . console . info ) . not . toBe ( originalConsole . info ) ;
57
- expect ( global . console . warn ) . not . toBe ( originalConsole . warn ) ;
58
- expect ( global . console . error ) . not . toBe ( originalConsole . error ) ;
59
- expect ( global . console . log ) . not . toBe ( originalConsole . log ) ;
60
- expect ( global . console . assert ) . not . toBe ( originalConsole . assert ) ;
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
+ } ) ;
61
69
62
- // any other fields should not have been patched
63
- expect ( global . console . trace ) . toBe ( originalConsole . trace ) ;
64
- expect ( global . console . table ) . toBe ( originalConsole . table ) ;
65
- } ) ;
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 ) as any ,
75
+ ) ;
66
76
67
- it ( 'should not wrap any functions with an empty levels option' , ( ) => {
68
- const captureConsoleIntegration = new CaptureConsole ( { levels : [ ] } ) ;
69
- captureConsoleIntegration . setupOnce (
70
- ( ) => undefined ,
71
- ( ) => getMockHubWithIntegration ( captureConsoleIntegration ) as any ,
72
- ) ;
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 ) as any ,
95
+ ) ;
73
96
74
- // expect the default set of console levels not to have been monkey patched
75
- expect ( global . console . debug ) . toBe ( originalConsole . debug ) ;
76
- expect ( global . console . info ) . toBe ( originalConsole . info ) ;
77
- expect ( global . console . warn ) . toBe ( originalConsole . warn ) ;
78
- expect ( global . console . error ) . toBe ( originalConsole . error ) ;
79
- expect ( global . console . log ) . toBe ( originalConsole . log ) ;
80
- expect ( global . console . assert ) . toBe ( originalConsole . assert ) ;
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 ) ;
81
104
82
- // expect no message to be captured with console.log
83
- global . console . log ( 'some message' ) ;
84
- expect ( mockHub . captureMessage ) . not . toHaveBeenCalled ( ) ;
105
+ // suppress output from the logging we're about to do
106
+ global . console . log = global . console . info = jest . fn ( ) ;
107
+
108
+ // expect no message to be captured with console.log
109
+ global . console . log ( 'some message' ) ;
110
+ expect ( mockHub . captureMessage ) . not . toHaveBeenCalled ( ) ;
111
+ } ) ;
85
112
} ) ;
86
113
87
114
it ( 'setup should fail gracefully when console is not available' , ( ) => {
0 commit comments