@@ -17,7 +17,8 @@ if (global.window) {
1717
1818// The issue only reproduced when React was loaded before JSDOM.
1919const React = require ( 'react' ) ;
20- const ReactDOM = require ( 'react-dom' ) ;
20+ const ReactDOMClient = require ( 'react-dom/client' ) ;
21+ const act = require ( 'internal-test-utils' ) . act ;
2122
2223// Initialize JSDOM separately.
2324// We don't use our normal JSDOM setup because we want to load React first.
@@ -43,13 +44,6 @@ describe('ReactErrorLoggingRecovery', () => {
4344
4445 beforeEach ( ( ) => {
4546 console . error = error => {
46- if (
47- typeof error === 'string' &&
48- error . includes ( 'ReactDOM.render is no longer supported in React 18' )
49- ) {
50- // Ignore legacy root deprecation warning
51- return ;
52- }
5347 throw new Error ( 'Buggy console.error' ) ;
5448 } ;
5549 } ) ;
@@ -58,23 +52,23 @@ describe('ReactErrorLoggingRecovery', () => {
5852 console . error = originalConsoleError ;
5953 } ) ;
6054
61- it ( 'should recover from errors in console.error' , function ( ) {
55+ it ( 'should recover from errors in console.error' , async function ( ) {
6256 const div = document . createElement ( 'div' ) ;
63- let didCatch = false ;
64- try {
65- ReactDOM . render ( < Bad /> , div ) ;
66- ReactDOM . render ( < Bad /> , div ) ;
67- } catch ( e ) {
68- expect ( e . message ) . toBe ( 'no' ) ;
69- didCatch = true ;
70- }
71- expect ( didCatch ) . toBe ( true ) ;
72- ReactDOM . render ( < span > Hello</ span > , div ) ;
73- expect ( div . firstChild . textContent ) . toBe ( 'Hello' ) ;
57+ const root = ReactDOMClient . createRoot ( div ) ;
58+ await expect ( async ( ) => {
59+ await act ( ( ) => {
60+ root . render ( < Bad /> ) ;
61+ } ) ;
62+ await act ( ( ) => {
63+ root . render ( < Bad /> ) ;
64+ } ) ;
65+ } ) . rejects . toThrow ( 'no' ) ;
7466
75- // Verify the console.error bug is surfaced
76- expect ( ( ) => {
77- jest . runAllTimers ( ) ;
78- } ) . toThrow ( 'Buggy console.error' ) ;
67+ await expect ( async ( ) => {
68+ await act ( ( ) => {
69+ root . render ( < span > Hello</ span > ) ;
70+ } ) ;
71+ } ) . rejects . toThrow ( 'Buggy console.error' ) ;
72+ expect ( div . firstChild . textContent ) . toBe ( 'Hello' ) ;
7973 } ) ;
8074} ) ;
0 commit comments