@@ -17,11 +17,10 @@ let mockLog;
1717let mockWarn ;
1818let patchConsole ;
1919let unpatchConsole ;
20+ let rendererID ;
2021
2122describe ( 'console' , ( ) => {
2223 beforeEach ( ( ) => {
23- jest . resetModules ( ) ;
24-
2524 const Console = require ( 'react-devtools-shared/src/backend/console' ) ;
2625 patchConsole = Console . patch ;
2726 unpatchConsole = Console . unpatch ;
@@ -41,21 +40,16 @@ describe('console', () => {
4140 } ;
4241
4342 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+ ) ;
5346
5447 const inject = global . __REACT_DEVTOOLS_GLOBAL_HOOK__ . inject ;
5548 global . __REACT_DEVTOOLS_GLOBAL_HOOK__ . inject = internals => {
56- inject ( internals ) ;
49+ rendererID = inject ( internals ) ;
5750
5851 Console . registerRenderer ( internals ) ;
52+ return rendererID ;
5953 } ;
6054
6155 React = require ( 'react' ) ;
@@ -78,7 +72,7 @@ describe('console', () => {
7872 it ( 'should not patch console methods that are not explicitly overriden' , ( ) => {
7973 expect ( fakeConsole . error ) . not . toBe ( mockError ) ;
8074 expect ( fakeConsole . info ) . toBe ( mockInfo ) ;
81- expect ( fakeConsole . log ) . not . toBe ( mockLog ) ;
75+ expect ( fakeConsole . log ) . toBe ( mockLog ) ;
8276 expect ( fakeConsole . warn ) . not . toBe ( mockWarn ) ;
8377 } ) ;
8478
@@ -90,23 +84,23 @@ describe('console', () => {
9084
9185 patchConsole ( {
9286 appendComponentStack : true ,
93- breakOnWarn : false ,
87+ breakOnConsoleErrors : false ,
9488 showInlineWarningsAndErrors : false ,
9589 } ) ;
9690
9791 expect ( fakeConsole . error ) . not . toBe ( mockError ) ;
9892 expect ( fakeConsole . warn ) . not . toBe ( mockWarn ) ;
9993 } ) ;
10094
101- it ( 'should patch the console when breakOnWarn is enabled' , ( ) => {
95+ it ( 'should patch the console when breakOnConsoleErrors is enabled' , ( ) => {
10296 unpatchConsole ( ) ;
10397
10498 expect ( fakeConsole . error ) . toBe ( mockError ) ;
10599 expect ( fakeConsole . warn ) . toBe ( mockWarn ) ;
106100
107101 patchConsole ( {
108102 appendComponentStack : false ,
109- breakOnWarn : true ,
103+ breakOnConsoleErrors : true ,
110104 showInlineWarningsAndErrors : false ,
111105 } ) ;
112106
@@ -122,7 +116,7 @@ describe('console', () => {
122116
123117 patchConsole ( {
124118 appendComponentStack : false ,
125- breakOnWarn : false ,
119+ breakOnConsoleErrors : false ,
126120 showInlineWarningsAndErrors : true ,
127121 } ) ;
128122
@@ -135,7 +129,7 @@ describe('console', () => {
135129
136130 patchConsole ( {
137131 appendComponentStack : true ,
138- breakOnWarn : false ,
132+ breakOnConsoleErrors : false ,
139133 showInlineWarningsAndErrors : false ,
140134 } ) ;
141135
@@ -172,6 +166,8 @@ describe('console', () => {
172166 } ) ;
173167
174168 it ( 'should not append multiple stacks' , ( ) => {
169+ global . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = true ;
170+
175171 const Child = ( { children} ) => {
176172 fakeConsole . warn ( 'warn\n in Child (at fake.js:123)' ) ;
177173 fakeConsole . error ( 'error' , '\n in Child (at fake.js:123)' ) ;
@@ -192,6 +188,8 @@ describe('console', () => {
192188 } ) ;
193189
194190 it ( 'should append component stacks to errors and warnings logged during render' , ( ) => {
191+ global . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = true ;
192+
195193 const Intermediate = ( { children} ) => children ;
196194 const Parent = ( { children} ) => (
197195 < Intermediate >
@@ -277,6 +275,8 @@ describe('console', () => {
277275 } ) ;
278276
279277 it ( 'should append component stacks to errors and warnings logged from commit hooks' , ( ) => {
278+ global . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = true ;
279+
280280 const Intermediate = ( { children} ) => children ;
281281 const Parent = ( { children} ) => (
282282 < Intermediate >
@@ -372,13 +372,14 @@ describe('console', () => {
372372 } ) ;
373373
374374 it ( 'should append stacks after being uninstalled and reinstalled' , ( ) => {
375+ global . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = false ;
376+
375377 const Child = ( { children} ) => {
376378 fakeConsole . warn ( 'warn' ) ;
377379 fakeConsole . error ( 'error' ) ;
378380 return null ;
379381 } ;
380382
381- unpatchConsole ( ) ;
382383 act ( ( ) => legacyRender ( < Child /> , document . createElement ( 'div' ) ) ) ;
383384
384385 expect ( mockWarn ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -390,7 +391,7 @@ describe('console', () => {
390391
391392 patchConsole ( {
392393 appendComponentStack : true ,
393- breakOnWarn : false ,
394+ breakOnConsoleErrors : false ,
394395 showInlineWarningsAndErrors : false ,
395396 } ) ;
396397 act ( ( ) => legacyRender ( < Child /> , document . createElement ( 'div' ) ) ) ;
@@ -410,6 +411,8 @@ describe('console', () => {
410411 } ) ;
411412
412413 it ( 'should be resilient to prepareStackTrace' , ( ) => {
414+ global . __REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = true ;
415+
413416 Error . prepareStackTrace = function ( error , callsites ) {
414417 const stack = [ 'An error occurred:' , error . message ] ;
415418 for ( let i = 0 ; i < callsites . length ; i ++ ) {
@@ -469,6 +472,9 @@ describe('console', () => {
469472 } ) ;
470473
471474 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+
472478 const container = document . createElement ( 'div' ) ;
473479 const root = ReactDOM . createRoot ( container ) ;
474480
@@ -479,22 +485,13 @@ describe('console', () => {
479485 return < div /> ;
480486 }
481487
482- patchConsole ( {
483- appendComponentStack : false ,
484- breakOnWarn : false ,
485- showInlineWarningsAndErrors : false ,
486- hideConsoleLogsInStrictMode : false ,
487- } ) ;
488-
489488 act ( ( ) =>
490489 root . render (
491490 < React . StrictMode >
492491 < App />
493492 </ React . StrictMode > ,
494493 ) ,
495494 ) ;
496-
497- expect ( mockLog ) . toHaveBeenCalledTimes ( 2 ) ;
498495 expect ( mockLog . mock . calls [ 0 ] ) . toHaveLength ( 1 ) ;
499496 expect ( mockLog . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'log' ) ;
500497 expect ( mockLog . mock . calls [ 1 ] ) . toHaveLength ( 2 ) ;
@@ -514,6 +511,9 @@ describe('console', () => {
514511 } ) ;
515512
516513 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+
517517 const container = document . createElement ( 'div' ) ;
518518 const root = ReactDOM . createRoot ( container ) ;
519519
@@ -524,12 +524,44 @@ describe('console', () => {
524524 return < div /> ;
525525 }
526526
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+ }
533565
534566 act ( ( ) =>
535567 root . render (
@@ -539,6 +571,50 @@ describe('console', () => {
539571 ) ,
540572 ) ;
541573
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+ ) ;
542618 expect ( mockLog ) . toHaveBeenCalledTimes ( 1 ) ;
543619 expect ( mockLog . mock . calls [ 0 ] ) . toHaveLength ( 1 ) ;
544620 expect ( mockLog . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'log' ) ;
@@ -577,23 +653,13 @@ describe('console error', () => {
577653
578654 Console . dangerous_setTargetConsoleForTesting ( fakeConsole ) ;
579655
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-
589656 const inject = global . __REACT_DEVTOOLS_GLOBAL_HOOK__ . inject ;
590657 global . __REACT_DEVTOOLS_GLOBAL_HOOK__ . inject = internals => {
591- internals . getIsStrictMode = ( ) => {
592- throw Error ( 'foo' ) ;
593- } ;
594658 inject ( internals ) ;
595659
596- Console . registerRenderer ( internals ) ;
660+ Console . registerRenderer ( internals , ( ) => {
661+ throw Error ( 'foo' ) ;
662+ } ) ;
597663 } ;
598664
599665 React = require ( 'react' ) ;
@@ -617,8 +683,8 @@ describe('console error', () => {
617683
618684 patchConsole ( {
619685 appendComponentStack : true ,
620- breakOnWarn : false ,
621- showInlineWarningsAndErrors : false ,
686+ breakOnConsoleErrors : false ,
687+ showInlineWarningsAndErrors : true ,
622688 hideConsoleLogsInStrictMode : false ,
623689 } ) ;
624690
0 commit comments