7
7
* @flow
8
8
*/
9
9
10
- import { getVersionedRenderImplementation , normalizeCodeLocInfo } from './utils' ;
10
+ import {
11
+ getVersionedRenderImplementation ,
12
+ normalizeCodeLocInfo ,
13
+ } from 'react-devtools-shared/src/__tests__/utils' ;
11
14
12
15
describe ( 'component stack' , ( ) => {
13
16
let React ;
14
17
let act ;
15
- let mockError ;
16
- let mockWarn ;
17
18
let supportsOwnerStacks ;
18
19
19
20
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
-
31
21
const utils = require ( './utils' ) ;
32
22
act = utils . act ;
33
23
@@ -54,18 +44,22 @@ describe('component stack', () => {
54
44
55
45
act ( ( ) => render ( < Grandparent /> ) ) ;
56
46
57
- expect ( mockError ) . toHaveBeenCalledWith (
47
+ expect (
48
+ global . consoleErrorMock . mock . calls [ 0 ] . map ( normalizeCodeLocInfo ) ,
49
+ ) . toEqual ( [
58
50
'Test error.' ,
59
51
'\n in Child (at **)' +
60
52
'\n in Parent (at **)' +
61
53
'\n in Grandparent (at **)' ,
62
- ) ;
63
- expect ( mockWarn ) . toHaveBeenCalledWith (
54
+ ] ) ;
55
+ expect (
56
+ global . consoleWarnMock . mock . calls [ 0 ] . map ( normalizeCodeLocInfo ) ,
57
+ ) . toEqual ( [
64
58
'Test warning.' ,
65
59
'\n in Child (at **)' +
66
60
'\n in Parent (at **)' +
67
61
'\n in Grandparent (at **)' ,
68
- ) ;
62
+ ] ) ;
69
63
} ) ;
70
64
71
65
// This test should have caught #19911
@@ -89,13 +83,15 @@ describe('component stack', () => {
89
83
90
84
expect ( useEffectCount ) . toBe ( 1 ) ;
91
85
92
- expect ( mockWarn ) . toHaveBeenCalledWith (
86
+ expect (
87
+ global . consoleWarnMock . mock . calls [ 0 ] . map ( normalizeCodeLocInfo ) ,
88
+ ) . toEqual ( [
93
89
'Warning to trigger appended component stacks.' ,
94
90
'\n in Example (at **)' ,
95
- ) ;
91
+ ] ) ;
96
92
} ) ;
97
93
98
- // @reactVersion >=18.3
94
+ // @reactVersion >= 18.3
99
95
it ( 'should log the current component stack with debug info from promises' , ( ) => {
100
96
const Child = ( ) => {
101
97
console . error ( 'Test error.' ) ;
@@ -117,23 +113,27 @@ describe('component stack', () => {
117
113
118
114
act ( ( ) => render ( < Grandparent /> ) ) ;
119
115
120
- expect ( mockError ) . toHaveBeenCalledWith (
116
+ expect (
117
+ global . consoleErrorMock . mock . calls [ 0 ] . map ( normalizeCodeLocInfo ) ,
118
+ ) . toEqual ( [
121
119
'Test error.' ,
122
120
supportsOwnerStacks
123
121
? '\n in Child (at **)'
124
122
: '\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 ( [
130
130
'Test warning.' ,
131
131
supportsOwnerStacks
132
132
? '\n in Child (at **)'
133
133
: '\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
+ ] ) ;
138
138
} ) ;
139
139
} ) ;
0 commit comments