77 * @flow
88 */
99
10- import { getVersionedRenderImplementation , normalizeCodeLocInfo } from './utils' ;
10+ import {
11+ getVersionedRenderImplementation ,
12+ normalizeCodeLocInfo ,
13+ } from 'react-devtools-shared/src/__tests__/utils' ;
1114
1215describe ( 'component stack' , ( ) => {
1316 let React ;
1417 let act ;
15- let mockError ;
16- let mockWarn ;
1718 let supportsOwnerStacks ;
1819
1920 beforeEach ( ( ) => {
20- // Intercept native console methods before DevTools bootstraps.
21- // Normalize component stack locations.
22- mockError = jest . fn ( ) ;
23- mockWarn = jest . fn ( ) ;
24- console . error = ( ...args ) => {
25- mockError ( ...args . map ( normalizeCodeLocInfo ) ) ;
26- } ;
27- console . warn = ( ...args ) => {
28- mockWarn ( ...args . map ( normalizeCodeLocInfo ) ) ;
29- } ;
30-
3121 const utils = require ( './utils' ) ;
3222 act = utils . act ;
3323
@@ -54,18 +44,22 @@ describe('component stack', () => {
5444
5545 act ( ( ) => render ( < Grandparent /> ) ) ;
5646
57- expect ( mockError ) . toHaveBeenCalledWith (
47+ expect (
48+ global . consoleErrorMock . mock . calls [ 0 ] . map ( normalizeCodeLocInfo ) ,
49+ ) . toEqual ( [
5850 'Test error.' ,
5951 '\n in Child (at **)' +
6052 '\n in Parent (at **)' +
6153 '\n in Grandparent (at **)' ,
62- ) ;
63- expect ( mockWarn ) . toHaveBeenCalledWith (
54+ ] ) ;
55+ expect (
56+ global . consoleWarnMock . mock . calls [ 0 ] . map ( normalizeCodeLocInfo ) ,
57+ ) . toEqual ( [
6458 'Test warning.' ,
6559 '\n in Child (at **)' +
6660 '\n in Parent (at **)' +
6761 '\n in Grandparent (at **)' ,
68- ) ;
62+ ] ) ;
6963 } ) ;
7064
7165 // This test should have caught #19911
@@ -89,13 +83,15 @@ describe('component stack', () => {
8983
9084 expect ( useEffectCount ) . toBe ( 1 ) ;
9185
92- expect ( mockWarn ) . toHaveBeenCalledWith (
86+ expect (
87+ global . consoleWarnMock . mock . calls [ 0 ] . map ( normalizeCodeLocInfo ) ,
88+ ) . toEqual ( [
9389 'Warning to trigger appended component stacks.' ,
9490 '\n in Example (at **)' ,
95- ) ;
91+ ] ) ;
9692 } ) ;
9793
98- // @reactVersion >=18.3
94+ // @reactVersion >= 18.3
9995 it ( 'should log the current component stack with debug info from promises' , ( ) => {
10096 const Child = ( ) => {
10197 console . error ( 'Test error.' ) ;
@@ -117,23 +113,27 @@ describe('component stack', () => {
117113
118114 act ( ( ) => render ( < Grandparent /> ) ) ;
119115
120- expect ( mockError ) . toHaveBeenCalledWith (
116+ expect (
117+ global . consoleErrorMock . mock . calls [ 0 ] . map ( normalizeCodeLocInfo ) ,
118+ ) . toEqual ( [
121119 'Test error.' ,
122120 supportsOwnerStacks
123121 ? '\n in Child (at **)'
124122 : '\n in Child (at **)' +
125- '\n in ServerComponent (at **)' +
126- '\n in Parent (at **)' +
127- '\n in Grandparent (at **)' ,
128- ) ;
129- expect ( mockWarn ) . toHaveBeenCalledWith (
123+ '\n in ServerComponent (at **)' +
124+ '\n in Parent (at **)' +
125+ '\n in Grandparent (at **)' ,
126+ ] ) ;
127+ expect (
128+ global . consoleWarnMock . mock . calls [ 0 ] . map ( normalizeCodeLocInfo ) ,
129+ ) . toEqual ( [
130130 'Test warning.' ,
131131 supportsOwnerStacks
132132 ? '\n in Child (at **)'
133133 : '\n in Child (at **)' +
134- '\n in ServerComponent (at **)' +
135- '\n in Parent (at **)' +
136- '\n in Grandparent (at **)' ,
137- ) ;
134+ '\n in ServerComponent (at **)' +
135+ '\n in Parent (at **)' +
136+ '\n in Grandparent (at **)' ,
137+ ] ) ;
138138 } ) ;
139139} ) ;
0 commit comments