11
11
'use strict' ;
12
12
13
13
let JSDOM ;
14
+ let JSDOMVirtualConsole ;
14
15
let Stream ;
15
16
let Scheduler ;
16
17
let React ;
@@ -32,38 +33,60 @@ let fatalError = undefined;
32
33
let originalDocument ;
33
34
let originalWindow ;
34
35
36
+ function resetModules ( ) {
37
+ jest . resetModules ( ) ;
38
+ ( { JSDOM , VirtualConsole : JSDOMVirtualConsole } = require ( 'jsdom' ) ) ;
39
+ Scheduler = require ( 'scheduler' ) ;
40
+ React = require ( 'react' ) ;
41
+ ReactDOMClient = require ( 'react-dom/client' ) ;
42
+ ReactDOMFizzServer = require ( 'react-dom/server' ) ;
43
+ Stream = require ( 'stream' ) ;
44
+ Suspense = React . Suspense ;
45
+ if ( gate ( flags => flags . enableSuspenseList ) ) {
46
+ SuspenseList = React . SuspenseList ;
47
+ use = React . experimental_use ;
48
+ }
49
+
50
+ PropTypes = require ( 'prop-types' ) ;
51
+
52
+ if ( gate ( flags => flags . source ) ) {
53
+ // The `with-selector` module composes the main `use-sync-external-store`
54
+ // entrypoint. In the compiled artifacts, this is resolved to the `shim`
55
+ // implementation by our build config, but when running the tests against
56
+ // the source files, we need to tell Jest how to resolve it. Because this
57
+ // is a source module, this mock has no affect on the build tests.
58
+ jest . mock ( 'use-sync-external-store/src/useSyncExternalStore' , ( ) =>
59
+ jest . requireActual ( 'react' ) ,
60
+ ) ;
61
+ }
62
+ useSyncExternalStore = React . useSyncExternalStore ;
63
+ useSyncExternalStoreWithSelector = require ( 'use-sync-external-store/with-selector' )
64
+ . useSyncExternalStoreWithSelector ;
65
+ }
66
+
67
+ function resetJSDOM ( markup ) {
68
+ // Test Environment
69
+ const virtualConsole = new JSDOMVirtualConsole ( ) ;
70
+ virtualConsole . sendTo ( console , {
71
+ omitJSDOMErrors : true ,
72
+ } ) ;
73
+ virtualConsole . on ( 'jsdomError' , error => {
74
+ console . error ( error ) ;
75
+ } ) ;
76
+ const jsdom = new JSDOM ( markup , {
77
+ runScripts : 'dangerously' ,
78
+ virtualConsole,
79
+ } ) ;
80
+ global . window = jsdom . window ;
81
+ global . document = jsdom . window . document ;
82
+ resetModules ( ) ;
83
+ }
84
+
35
85
describe ( 'ReactDOMFizzServer' , ( ) => {
36
86
beforeEach ( ( ) => {
37
- jest . resetModules ( ) ;
38
87
originalDocument = global . document ;
39
88
originalWindow = global . window ;
40
- JSDOM = require ( 'jsdom' ) . JSDOM ;
41
- Scheduler = require ( 'scheduler' ) ;
42
- React = require ( 'react' ) ;
43
- ReactDOMClient = require ( 'react-dom/client' ) ;
44
- ReactDOMFizzServer = require ( 'react-dom/server' ) ;
45
- Stream = require ( 'stream' ) ;
46
- Suspense = React . Suspense ;
47
- if ( gate ( flags => flags . enableSuspenseList ) ) {
48
- SuspenseList = React . SuspenseList ;
49
- use = React . experimental_use ;
50
- }
51
-
52
- PropTypes = require ( 'prop-types' ) ;
53
-
54
- if ( gate ( flags => flags . source ) ) {
55
- // The `with-selector` module composes the main `use-sync-external-store`
56
- // entrypoint. In the compiled artifacts, this is resolved to the `shim`
57
- // implementation by our build config, but when running the tests against
58
- // the source files, we need to tell Jest how to resolve it. Because this
59
- // is a source module, this mock has no affect on the build tests.
60
- jest . mock ( 'use-sync-external-store/src/useSyncExternalStore' , ( ) =>
61
- jest . requireActual ( 'react' ) ,
62
- ) ;
63
- }
64
- useSyncExternalStore = React . useSyncExternalStore ;
65
- useSyncExternalStoreWithSelector = require ( 'use-sync-external-store/with-selector' )
66
- . useSyncExternalStoreWithSelector ;
89
+ resetModules ( ) ;
67
90
68
91
textCache = new Map ( ) ;
69
92
@@ -167,11 +190,8 @@ describe('ReactDOMFizzServer', () => {
167
190
// We assume that we have now received a proper fragment of HTML.
168
191
const bufferedContent = buffer ;
169
192
// Test Environment
170
- const jsdom = new JSDOM ( bufferedContent , {
171
- runScripts : 'dangerously' ,
172
- } ) ;
173
- global . window = jsdom . window ;
174
- global . document = jsdom . window . document ;
193
+ resetJSDOM ( bufferedContent ) ;
194
+
175
195
container = document ;
176
196
buffer = '' ;
177
197
}
@@ -4367,11 +4387,9 @@ describe('ReactDOMFizzServer', () => {
4367
4387
try {
4368
4388
expect ( ( ) => {
4369
4389
expect ( Scheduler ) . toFlushWithoutYielding ( ) ;
4370
- } ) . toThrowErrorMatchingInlineSnapshot (
4371
- `"Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'."` ,
4372
- ) ;
4373
- } catch ( e ) {
4374
- console . log ( 'e' , e ) ;
4390
+ } ) . toThrow ( 'Invalid insertion of HTML node in #document node.' ) ;
4391
+ } catch ( error ) {
4392
+ console . log ( 'e' , error ) ;
4375
4393
}
4376
4394
} ) . toErrorDev (
4377
4395
[
@@ -4381,17 +4399,14 @@ describe('ReactDOMFizzServer', () => {
4381
4399
] ,
4382
4400
{ withoutStack : 1 } ,
4383
4401
) ;
4384
- expect ( errors ) . toMatchInlineSnapshot ( `Array []` ) ;
4385
- expect ( getVisibleChildren ( document ) ) . toMatchInlineSnapshot ( `
4386
- <title
4387
- data-baz="baz"
4388
- >
4389
- a title
4390
- </title>
4391
- ` ) ;
4402
+ expect ( errors ) . toEqual ( [
4403
+ 'Hydration failed because the initial UI does not match what was rendered on the server.' ,
4404
+ 'There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.' ,
4405
+ ] ) ;
4406
+ expect ( getVisibleChildren ( document ) ) . toEqual ( ) ;
4392
4407
expect ( ( ) => {
4393
4408
expect ( Scheduler ) . toFlushWithoutYielding ( ) ;
4394
- } ) . not . toThrow ( ) ;
4409
+ } ) . toThrow ( 'The node to be removed is not a child of this node.' ) ;
4395
4410
} ) ;
4396
4411
4397
4412
// @gate enableFloat
@@ -5113,11 +5128,7 @@ describe('ReactDOMFizzServer', () => {
5113
5128
describe ( 'title children' , ( ) => {
5114
5129
function prepareJSDOMForTitle ( ) {
5115
5130
// Test Environment
5116
- const jsdom = new JSDOM ( '<!DOCTYPE html><html><head>\u0000' , {
5117
- runScripts : 'dangerously' ,
5118
- } ) ;
5119
- global . window = jsdom . window ;
5120
- global . document = jsdom . window . document ;
5131
+ resetJSDOM ( '<!DOCTYPE html><html><head>\u0000' ) ;
5121
5132
container = document . getElementsByTagName ( 'head' ) [ 0 ] ;
5122
5133
}
5123
5134
0 commit comments