From 601dba82175edd321c833395514081fe3aa6f445 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Fri, 19 Jan 2024 18:07:08 +0100 Subject: [PATCH] Convert ReactErrorLoggingRecovery to createRoot (#28003) --- .../ReactErrorLoggingRecovery-test.js | 42 ++++++++----------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactErrorLoggingRecovery-test.js b/packages/react-dom/src/__tests__/ReactErrorLoggingRecovery-test.js index 0721e3db4dea4..388ac58f3bb2e 100644 --- a/packages/react-dom/src/__tests__/ReactErrorLoggingRecovery-test.js +++ b/packages/react-dom/src/__tests__/ReactErrorLoggingRecovery-test.js @@ -17,7 +17,8 @@ if (global.window) { // The issue only reproduced when React was loaded before JSDOM. const React = require('react'); -const ReactDOM = require('react-dom'); +const ReactDOMClient = require('react-dom/client'); +const act = require('internal-test-utils').act; // Initialize JSDOM separately. // We don't use our normal JSDOM setup because we want to load React first. @@ -43,13 +44,6 @@ describe('ReactErrorLoggingRecovery', () => { beforeEach(() => { console.error = error => { - if ( - typeof error === 'string' && - error.includes('ReactDOM.render is no longer supported in React 18') - ) { - // Ignore legacy root deprecation warning - return; - } throw new Error('Buggy console.error'); }; }); @@ -58,23 +52,23 @@ describe('ReactErrorLoggingRecovery', () => { console.error = originalConsoleError; }); - it('should recover from errors in console.error', function () { + it('should recover from errors in console.error', async function () { const div = document.createElement('div'); - let didCatch = false; - try { - ReactDOM.render(, div); - ReactDOM.render(, div); - } catch (e) { - expect(e.message).toBe('no'); - didCatch = true; - } - expect(didCatch).toBe(true); - ReactDOM.render(Hello, div); - expect(div.firstChild.textContent).toBe('Hello'); + const root = ReactDOMClient.createRoot(div); + await expect(async () => { + await act(() => { + root.render(); + }); + await act(() => { + root.render(); + }); + }).rejects.toThrow('no'); - // Verify the console.error bug is surfaced - expect(() => { - jest.runAllTimers(); - }).toThrow('Buggy console.error'); + await expect(async () => { + await act(() => { + root.render(Hello); + }); + }).rejects.toThrow('Buggy console.error'); + expect(div.firstChild.textContent).toBe('Hello'); }); });