Skip to content

Commit

Permalink
Add ReactDOMClient to ServerIntegrationTestUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
rickhanlonii committed Jan 27, 2024
1 parent 9aef5d2 commit 233b65d
Show file tree
Hide file tree
Showing 12 changed files with 77 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,20 @@ module.exports = function (initModules) {
async function asyncReactDOMRender(reactElement, domElement, forceHydrate) {
if (forceHydrate) {
await act(() => {
ReactDOM.hydrate(reactElement, domElement);
if (ReactDOMClient) {
ReactDOMClient.hydrateRoot(domElement, reactElement);
} 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 +90,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
30 changes: 29 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') !== -1
) {
// When switching to createRoot, this error isn't accounted for.
// Rather than update all tests, we're ignoring it. But we should
// updated the tests to account for this error.
return true;
}
} else if (
format != null &&
typeof format.message === 'string' &&
Expand All @@ -34,6 +47,21 @@ module.exports = function shouldIgnoreConsoleError(format, args) {
// in development that is reported by jest-environment-jsdom. Ignore because it's noisy.
return true;
}

if (
(TODO_ignoreHydrationErrors &&
format.message.indexOf('Hydration failed because') !== -1) ||
format.message.indexOf(
'Text content does not match server-rendered HTML'
) !== -1 ||
format.message.indexOf('There was an error while hydrating.') !== -1 ||
format.message.indexOf('An error occurred during hydration') !== -1
) {
// When switching to createRoot, these errors aren't accounted for.
// Rather than update all tests, we're ignoring them. But we should
// updated the tests to account for these errors.
return true;
}
}
} else {
if (
Expand Down

0 comments on commit 233b65d

Please sign in to comment.