@@ -17,11 +17,10 @@ let mockLog;
17
17
let mockWarn ;
18
18
let patchConsole ;
19
19
let unpatchConsole ;
20
+ let rendererID ;
20
21
21
22
describe ( 'console' , ( ) => {
22
23
beforeEach ( ( ) => {
23
- jest . resetModules ( ) ;
24
-
25
24
const Console = require ( 'react-devtools-shared/src/backend/console' ) ;
26
25
patchConsole = Console . patch ;
27
26
unpatchConsole = Console . unpatch ;
@@ -41,21 +40,16 @@ describe('console', () => {
41
40
} ;
42
41
43
42
Console . dangerous_setTargetConsoleForTesting ( fakeConsole ) ;
44
-
45
- // Note the Console module only patches once,
46
- // so it's important to patch the test console before injection.
47
- patchConsole ( {
48
- appendComponentStack : true ,
49
- breakOnWarn : false ,
50
- showInlineWarningsAndErrors : false ,
51
- hideDoubleLogsInStrictLegacy : false ,
52
- } ) ;
43
+ global . __REACT_DEVTOOLS_GLOBAL_HOOK__ . dangerous_setTargetConsoleForTesting (
44
+ fakeConsole ,
45
+ ) ;
53
46
54
47
const inject = global . __REACT_DEVTOOLS_GLOBAL_HOOK__ . inject ;
55
48
global . __REACT_DEVTOOLS_GLOBAL_HOOK__ . inject = internals => {
56
- inject ( internals ) ;
49
+ rendererID = inject ( internals ) ;
57
50
58
51
Console . registerRenderer ( internals ) ;
52
+ return rendererID ;
59
53
} ;
60
54
61
55
React = require ( 'react' ) ;
@@ -78,7 +72,7 @@ describe('console', () => {
78
72
it ( 'should not patch console methods that are not explicitly overriden' , ( ) => {
79
73
expect ( fakeConsole . error ) . not . toBe ( mockError ) ;
80
74
expect ( fakeConsole . info ) . toBe ( mockInfo ) ;
81
- expect ( fakeConsole . log ) . not . toBe ( mockLog ) ;
75
+ expect ( fakeConsole . log ) . toBe ( mockLog ) ;
82
76
expect ( fakeConsole . warn ) . not . toBe ( mockWarn ) ;
83
77
} ) ;
84
78
@@ -90,23 +84,23 @@ describe('console', () => {
90
84
91
85
patchConsole ( {
92
86
appendComponentStack : true ,
93
- breakOnWarn : false ,
87
+ breakOnConsoleErrors : false ,
94
88
showInlineWarningsAndErrors : false ,
95
89
} ) ;
96
90
97
91
expect ( fakeConsole . error ) . not . toBe ( mockError ) ;
98
92
expect ( fakeConsole . warn ) . not . toBe ( mockWarn ) ;
99
93
} ) ;
100
94
101
- it ( 'should patch the console when breakOnWarn is enabled' , ( ) => {
95
+ it ( 'should patch the console when breakOnConsoleErrors is enabled' , ( ) => {
102
96
unpatchConsole ( ) ;
103
97
104
98
expect ( fakeConsole . error ) . toBe ( mockError ) ;
105
99
expect ( fakeConsole . warn ) . toBe ( mockWarn ) ;
106
100
107
101
patchConsole ( {
108
102
appendComponentStack : false ,
109
- breakOnWarn : true ,
103
+ breakOnConsoleErrors : true ,
110
104
showInlineWarningsAndErrors : false ,
111
105
} ) ;
112
106
@@ -122,7 +116,7 @@ describe('console', () => {
122
116
123
117
patchConsole ( {
124
118
appendComponentStack : false ,
125
- breakOnWarn : false ,
119
+ breakOnConsoleErrors : false ,
126
120
showInlineWarningsAndErrors : true ,
127
121
} ) ;
128
122
@@ -135,7 +129,7 @@ describe('console', () => {
135
129
136
130
patchConsole ( {
137
131
appendComponentStack : true ,
138
- breakOnWarn : false ,
132
+ breakOnConsoleErrors : false ,
139
133
showInlineWarningsAndErrors : false ,
140
134
} ) ;
141
135
@@ -172,6 +166,8 @@ describe('console', () => {
172
166
} ) ;
173
167
174
168
it ( 'should not append multiple stacks' , ( ) => {
169
+ global . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = true ;
170
+
175
171
const Child = ( { children} ) => {
176
172
fakeConsole . warn ( 'warn\n in Child (at fake.js:123)' ) ;
177
173
fakeConsole . error ( 'error' , '\n in Child (at fake.js:123)' ) ;
@@ -192,6 +188,8 @@ describe('console', () => {
192
188
} ) ;
193
189
194
190
it ( 'should append component stacks to errors and warnings logged during render' , ( ) => {
191
+ global . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = true ;
192
+
195
193
const Intermediate = ( { children} ) => children ;
196
194
const Parent = ( { children} ) => (
197
195
< Intermediate >
@@ -277,6 +275,8 @@ describe('console', () => {
277
275
} ) ;
278
276
279
277
it ( 'should append component stacks to errors and warnings logged from commit hooks' , ( ) => {
278
+ global . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = true ;
279
+
280
280
const Intermediate = ( { children} ) => children ;
281
281
const Parent = ( { children} ) => (
282
282
< Intermediate >
@@ -372,13 +372,14 @@ describe('console', () => {
372
372
} ) ;
373
373
374
374
it ( 'should append stacks after being uninstalled and reinstalled' , ( ) => {
375
+ global . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = false ;
376
+
375
377
const Child = ( { children} ) => {
376
378
fakeConsole . warn ( 'warn' ) ;
377
379
fakeConsole . error ( 'error' ) ;
378
380
return null ;
379
381
} ;
380
382
381
- unpatchConsole ( ) ;
382
383
act ( ( ) => legacyRender ( < Child /> , document . createElement ( 'div' ) ) ) ;
383
384
384
385
expect ( mockWarn ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -390,7 +391,7 @@ describe('console', () => {
390
391
391
392
patchConsole ( {
392
393
appendComponentStack : true ,
393
- breakOnWarn : false ,
394
+ breakOnConsoleErrors : false ,
394
395
showInlineWarningsAndErrors : false ,
395
396
} ) ;
396
397
act ( ( ) => legacyRender ( < Child /> , document . createElement ( 'div' ) ) ) ;
@@ -410,6 +411,8 @@ describe('console', () => {
410
411
} ) ;
411
412
412
413
it ( 'should be resilient to prepareStackTrace' , ( ) => {
414
+ global . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = true ;
415
+
413
416
Error . prepareStackTrace = function ( error , callsites ) {
414
417
const stack = [ 'An error occurred:' , error . message ] ;
415
418
for ( let i = 0 ; i < callsites . length ; i ++ ) {
@@ -469,6 +472,9 @@ describe('console', () => {
469
472
} ) ;
470
473
471
474
it ( 'should double log if hideConsoleLogsInStrictMode is disabled in Strict mode' , ( ) => {
475
+ global . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = false ;
476
+ global . __REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ = false ;
477
+
472
478
const container = document . createElement ( 'div' ) ;
473
479
const root = ReactDOM . createRoot ( container ) ;
474
480
@@ -479,22 +485,13 @@ describe('console', () => {
479
485
return < div /> ;
480
486
}
481
487
482
- patchConsole ( {
483
- appendComponentStack : false ,
484
- breakOnWarn : false ,
485
- showInlineWarningsAndErrors : false ,
486
- hideConsoleLogsInStrictMode : false ,
487
- } ) ;
488
-
489
488
act ( ( ) =>
490
489
root . render (
491
490
< React . StrictMode >
492
491
< App />
493
492
</ React . StrictMode > ,
494
493
) ,
495
494
) ;
496
-
497
- expect ( mockLog ) . toHaveBeenCalledTimes ( 2 ) ;
498
495
expect ( mockLog . mock . calls [ 0 ] ) . toHaveLength ( 1 ) ;
499
496
expect ( mockLog . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'log' ) ;
500
497
expect ( mockLog . mock . calls [ 1 ] ) . toHaveLength ( 2 ) ;
@@ -514,6 +511,9 @@ describe('console', () => {
514
511
} ) ;
515
512
516
513
it ( 'should not double log if hideConsoleLogsInStrictMode is enabled in Strict mode' , ( ) => {
514
+ global . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = false ;
515
+ global . __REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ = true ;
516
+
517
517
const container = document . createElement ( 'div' ) ;
518
518
const root = ReactDOM . createRoot ( container ) ;
519
519
@@ -524,12 +524,44 @@ describe('console', () => {
524
524
return < div /> ;
525
525
}
526
526
527
- patchConsole ( {
528
- appendComponentStack : false ,
529
- breakOnWarn : false ,
530
- showInlineWarningsAndErrors : false ,
531
- hideConsoleLogsInStrictMode : true ,
532
- } ) ;
527
+ act ( ( ) =>
528
+ root . render (
529
+ < React . StrictMode >
530
+ < App />
531
+ </ React . StrictMode > ,
532
+ ) ,
533
+ ) ;
534
+
535
+ expect ( mockLog ) . toHaveBeenCalledTimes ( 1 ) ;
536
+ expect ( mockLog . mock . calls [ 0 ] ) . toHaveLength ( 1 ) ;
537
+ expect ( mockLog . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'log' ) ;
538
+
539
+ expect ( mockWarn ) . toHaveBeenCalledTimes ( 1 ) ;
540
+ expect ( mockWarn . mock . calls [ 0 ] ) . toHaveLength ( 1 ) ;
541
+ expect ( mockWarn . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'warn' ) ;
542
+
543
+ expect ( mockError ) . toHaveBeenCalledTimes ( 1 ) ;
544
+ expect ( mockError . mock . calls [ 0 ] ) . toHaveLength ( 1 ) ;
545
+ expect ( mockError . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'error' ) ;
546
+ } ) ;
547
+
548
+ it ( 'should double log in Strict mode initial render for extension' , ( ) => {
549
+ global . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = false ;
550
+ global . __REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ = false ;
551
+
552
+ global . __REACT_DEVTOOLS_GLOBAL_HOOK__ . rendererInterfaces . set (
553
+ rendererID ,
554
+ null ,
555
+ ) ;
556
+ const container = document . createElement ( 'div' ) ;
557
+ const root = ReactDOM . createRoot ( container ) ;
558
+
559
+ function App ( ) {
560
+ fakeConsole . log ( 'log' ) ;
561
+ fakeConsole . warn ( 'warn' ) ;
562
+ fakeConsole . error ( 'error' ) ;
563
+ return < div /> ;
564
+ }
533
565
534
566
act ( ( ) =>
535
567
root . render (
@@ -539,6 +571,50 @@ describe('console', () => {
539
571
) ,
540
572
) ;
541
573
574
+ expect ( mockLog ) . toHaveBeenCalledTimes ( 2 ) ;
575
+ expect ( mockLog . mock . calls [ 0 ] ) . toHaveLength ( 1 ) ;
576
+ expect ( mockLog . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'log' ) ;
577
+ expect ( mockLog . mock . calls [ 1 ] ) . toHaveLength ( 2 ) ;
578
+ expect ( mockLog . mock . calls [ 1 ] [ 0 ] ) . toBe ( '%clog' ) ;
579
+
580
+ expect ( mockWarn ) . toHaveBeenCalledTimes ( 2 ) ;
581
+ expect ( mockWarn . mock . calls [ 0 ] ) . toHaveLength ( 1 ) ;
582
+ expect ( mockWarn . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'warn' ) ;
583
+ expect ( mockWarn . mock . calls [ 1 ] ) . toHaveLength ( 2 ) ;
584
+ expect ( mockWarn . mock . calls [ 1 ] [ 0 ] ) . toBe ( '%cwarn' ) ;
585
+
586
+ expect ( mockError ) . toHaveBeenCalledTimes ( 2 ) ;
587
+ expect ( mockError . mock . calls [ 0 ] ) . toHaveLength ( 1 ) ;
588
+ expect ( mockError . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'error' ) ;
589
+ expect ( mockError . mock . calls [ 1 ] ) . toHaveLength ( 2 ) ;
590
+ expect ( mockError . mock . calls [ 1 ] [ 0 ] ) . toBe ( '%cerror' ) ;
591
+ } ) ;
592
+
593
+ it ( 'should not double log in Strict mode initial render for extension' , ( ) => {
594
+ global . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = false ;
595
+ global . __REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ = true ;
596
+
597
+ global . __REACT_DEVTOOLS_GLOBAL_HOOK__ . rendererInterfaces . set (
598
+ rendererID ,
599
+ null ,
600
+ ) ;
601
+ const container = document . createElement ( 'div' ) ;
602
+ const root = ReactDOM . createRoot ( container ) ;
603
+
604
+ function App ( ) {
605
+ fakeConsole . log ( 'log' ) ;
606
+ fakeConsole . warn ( 'warn' ) ;
607
+ fakeConsole . error ( 'error' ) ;
608
+ return < div /> ;
609
+ }
610
+
611
+ act ( ( ) =>
612
+ root . render (
613
+ < React . StrictMode >
614
+ < App />
615
+ </ React . StrictMode > ,
616
+ ) ,
617
+ ) ;
542
618
expect ( mockLog ) . toHaveBeenCalledTimes ( 1 ) ;
543
619
expect ( mockLog . mock . calls [ 0 ] ) . toHaveLength ( 1 ) ;
544
620
expect ( mockLog . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'log' ) ;
@@ -577,23 +653,13 @@ describe('console error', () => {
577
653
578
654
Console . dangerous_setTargetConsoleForTesting ( fakeConsole ) ;
579
655
580
- // Note the Console module only patches once,
581
- // so it's important to patch the test console before injection.
582
- patchConsole ( {
583
- appendComponentStack : true ,
584
- breakOnWarn : false ,
585
- showInlineWarningsAndErrors : false ,
586
- hideDoubleLogsInStrictLegacy : false ,
587
- } ) ;
588
-
589
656
const inject = global . __REACT_DEVTOOLS_GLOBAL_HOOK__ . inject ;
590
657
global . __REACT_DEVTOOLS_GLOBAL_HOOK__ . inject = internals => {
591
- internals . getIsStrictMode = ( ) => {
592
- throw Error ( 'foo' ) ;
593
- } ;
594
658
inject ( internals ) ;
595
659
596
- Console . registerRenderer ( internals ) ;
660
+ Console . registerRenderer ( internals , ( ) => {
661
+ throw Error ( 'foo' ) ;
662
+ } ) ;
597
663
} ;
598
664
599
665
React = require ( 'react' ) ;
@@ -617,8 +683,8 @@ describe('console error', () => {
617
683
618
684
patchConsole ( {
619
685
appendComponentStack : true ,
620
- breakOnWarn : false ,
621
- showInlineWarningsAndErrors : false ,
686
+ breakOnConsoleErrors : false ,
687
+ showInlineWarningsAndErrors : true ,
622
688
hideConsoleLogsInStrictMode : false ,
623
689
} ) ;
624
690
0 commit comments