Skip to content

Commit a538460

Browse files
committed
We'll get there
1 parent 3faa3ee commit a538460

File tree

1 file changed

+63
-52
lines changed

1 file changed

+63
-52
lines changed

packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js

Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'use strict';
1212

1313
let JSDOM;
14+
let JSDOMVirtualConsole;
1415
let Stream;
1516
let Scheduler;
1617
let React;
@@ -32,38 +33,60 @@ let fatalError = undefined;
3233
let originalDocument;
3334
let originalWindow;
3435

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+
3585
describe('ReactDOMFizzServer', () => {
3686
beforeEach(() => {
37-
jest.resetModules();
3887
originalDocument = global.document;
3988
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();
6790

6891
textCache = new Map();
6992

@@ -167,11 +190,8 @@ describe('ReactDOMFizzServer', () => {
167190
// We assume that we have now received a proper fragment of HTML.
168191
const bufferedContent = buffer;
169192
// 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+
175195
container = document;
176196
buffer = '';
177197
}
@@ -4367,11 +4387,9 @@ describe('ReactDOMFizzServer', () => {
43674387
try {
43684388
expect(() => {
43694389
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);
43754393
}
43764394
}).toErrorDev(
43774395
[
@@ -4381,17 +4399,14 @@ describe('ReactDOMFizzServer', () => {
43814399
],
43824400
{withoutStack: 1},
43834401
);
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();
43924407
expect(() => {
43934408
expect(Scheduler).toFlushWithoutYielding();
4394-
}).not.toThrow();
4409+
}).toThrow('The node to be removed is not a child of this node.');
43954410
});
43964411

43974412
// @gate enableFloat
@@ -5113,11 +5128,7 @@ describe('ReactDOMFizzServer', () => {
51135128
describe('title children', () => {
51145129
function prepareJSDOMForTitle() {
51155130
// 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');
51215132
container = document.getElementsByTagName('head')[0];
51225133
}
51235134

0 commit comments

Comments
 (0)