Skip to content

Commit

Permalink
Add ReactDOMClient to ServerIntegrationTestUtils (#28130)
Browse files Browse the repository at this point in the history
## Overview

Adds support for `ReactDOMClient` for  `ServerIntegration*` tests. 

Also converts tests that pass without any other changes. Will follow up
with other PRs for more complex cases.
  • Loading branch information
rickhanlonii authored and gaearon committed Feb 3, 2024
1 parent 9177938 commit 5c8fd5d
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegratio
const {disableInputAttributeSyncing} = require('shared/ReactFeatureFlags');

let React;
let ReactDOM;
let ReactDOMClient;
let ReactDOMServer;
let ReactTestUtils;

function initModules() {
// Reset warning cache.
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
ReactTestUtils = require('react-dom/test-utils');

// Make them available to the helpers.
return {
ReactDOM,
ReactDOMClient,
ReactDOMServer,
ReactTestUtils,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils');

let React;
let ReactDOM;
let ReactDOMClient;
let ReactDOMServer;
let ReactTestUtils;

function initModules() {
// Reset warning cache.
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
ReactTestUtils = require('react-dom/test-utils');

// Make them available to the helpers.
return {
ReactDOM,
ReactDOMClient,
ReactDOMServer,
ReactTestUtils,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils');

let React;
let ReactDOM;
let ReactDOMClient;
let ReactDOMServer;
let ReactTestUtils;

function initModules() {
// Reset warning cache.
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
ReactTestUtils = require('react-dom/test-utils');

// Make them available to the helpers.
return {
ReactDOM,
ReactDOMClient,
ReactDOMServer,
ReactTestUtils,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegratio
const {disableInputAttributeSyncing} = require('shared/ReactFeatureFlags');

let React;
let ReactDOM;
let ReactDOMClient;
let ReactDOMServer;
let ReactTestUtils;

function initModules() {
// Reset warning cache.
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
ReactTestUtils = require('react-dom/test-utils');

// Make them available to the helpers.
return {
ReactDOM,
ReactDOMClient,
ReactDOMServer,
ReactTestUtils,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils');

let React;
let ReactDOM;
let ReactDOMClient;
let ReactDOMServer;
let ReactTestUtils;

function initModules() {
// Reset warning cache.
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
ReactTestUtils = require('react-dom/test-utils');

// Make them available to the helpers.
return {
ReactDOM,
ReactDOMClient,
ReactDOMServer,
ReactTestUtils,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils');

let React;
let ReactDOM;
let ReactDOMClient;
let ReactDOMServer;
let ReactTestUtils;

function initModules() {
// Reset warning cache.
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
ReactTestUtils = require('react-dom/test-utils');

// Make them available to the helpers.
return {
ReactDOM,
ReactDOMClient,
ReactDOMServer,
ReactTestUtils,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils');

let React;
let ReactDOM;
let ReactDOMClient;
let ReactDOMServer;
let ReactTestUtils;
let forwardRef;
Expand All @@ -26,7 +26,7 @@ function initModules() {
// Reset warning cache.
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
ReactTestUtils = require('react-dom/test-utils');
forwardRef = React.forwardRef;
Expand All @@ -44,7 +44,7 @@ function initModules() {

// Make them available to the helpers.
return {
ReactDOM,
ReactDOMClient,
ReactDOMServer,
ReactTestUtils,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils');

let React;
let ReactDOM;
let ReactDOMClient;
let ReactDOMServer;
let ReactTestUtils;

function initModules() {
// Reset warning cache.
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
ReactTestUtils = require('react-dom/test-utils');

// Make them available to the helpers.
return {
ReactDOM,
ReactDOMClient,
ReactDOMServer,
ReactTestUtils,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils');

let React;
let ReactDOM;
let ReactDOMClient;
let ReactDOMServer;
let ReactTestUtils;

function initModules() {
// Reset warning cache.
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
ReactTestUtils = require('react-dom/test-utils');

// Make them available to the helpers.
return {
ReactDOM,
ReactDOMClient,
ReactDOMServer,
ReactTestUtils,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils');

let React;
let ReactDOM;
let ReactDOMClient;
let ReactDOMServer;
let ReactTestUtils;

function initModules() {
// Reset warning cache.
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
ReactTestUtils = require('react-dom/test-utils');

// Make them available to the helpers.
return {
ReactDOM,
ReactDOMClient,
ReactDOMServer,
ReactTestUtils,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ const shouldIgnoreConsoleError = require('../../../../../scripts/jest/shouldIgno

module.exports = function (initModules) {
let ReactDOM;
let ReactDOMClient;
let ReactDOMServer;
let act;

function resetModules() {
({ReactDOM, ReactDOMServer} = initModules());
({ReactDOM, ReactDOMClient, ReactDOMServer} = initModules());
act = require('internal-test-utils').act;
}

Expand Down Expand Up @@ -51,11 +52,24 @@ module.exports = function (initModules) {
async function asyncReactDOMRender(reactElement, domElement, forceHydrate) {
if (forceHydrate) {
await act(() => {
ReactDOM.hydrate(reactElement, domElement);
if (ReactDOMClient) {
ReactDOMClient.hydrateRoot(domElement, reactElement, {
onRecoverableError: () => {
// TODO: assert on recoverable error count.
},
});
} else {
ReactDOM.hydrate(reactElement, domElement);
}
});
} else {
await act(() => {
ReactDOM.render(reactElement, domElement);
if (ReactDOMClient) {
const root = ReactDOMClient.createRoot(domElement);
root.render(reactElement);
} else {
ReactDOM.render(reactElement, domElement);
}
});
}
}
Expand All @@ -80,7 +94,11 @@ module.exports = function (initModules) {
for (let i = 0; i < console.error.mock.calls.length; i++) {
const args = console.error.mock.calls[i];
const [format, ...rest] = args;
if (!shouldIgnoreConsoleError(format, rest)) {
if (
!shouldIgnoreConsoleError(format, rest, {
TODO_ignoreHydrationErrors: true,
})
) {
filteredWarnings.push(args);
}
}
Expand Down
15 changes: 14 additions & 1 deletion scripts/jest/shouldIgnoreConsoleError.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use strict';

module.exports = function shouldIgnoreConsoleError(format, args) {
module.exports = function shouldIgnoreConsoleError(
format,
args,
{TODO_ignoreHydrationErrors} = {TODO_ignoreHydrationErrors: false}
) {
if (__DEV__) {
if (typeof format === 'string') {
if (format.indexOf('Error: Uncaught [') === 0) {
Expand All @@ -23,6 +27,15 @@ module.exports = function shouldIgnoreConsoleError(format, args) {
// We haven't finished migrating our tests to use createRoot.
return true;
}
if (
TODO_ignoreHydrationErrors &&
format.indexOf(
'An error occurred during hydration. The server HTML was replaced with client content in'
) !== -1
) {
// This also gets logged by onRecoverableError, so we can ignore it.
return true;
}
} else if (
format != null &&
typeof format.message === 'string' &&
Expand Down

0 comments on commit 5c8fd5d

Please sign in to comment.