From 7e5fa1b1d2a9b4e7194471a376e444cb20db2c8c Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Fri, 10 Mar 2023 17:14:44 -0500 Subject: [PATCH] Update Float tests to check for specific errors I updated some of the Float tests that intentionally trigger an error to assert on the specific error message, rather than swallow any errors that may or may not happen. --- .../ReactInternalTestUtils.js | 12 +- .../src/__tests__/ReactDOMFloat-test.js | 192 +++++++++--------- 2 files changed, 109 insertions(+), 95 deletions(-) diff --git a/packages/internal-test-utils/ReactInternalTestUtils.js b/packages/internal-test-utils/ReactInternalTestUtils.js index 3051cefaedd64..61ad293db72c1 100644 --- a/packages/internal-test-utils/ReactInternalTestUtils.js +++ b/packages/internal-test-utils/ReactInternalTestUtils.js @@ -104,7 +104,7 @@ ${diff(expectedLog, actualLog)} throw error; } -export async function waitForThrow(expectedError: mixed) { +export async function waitForThrow(expectedError: mixed): mixed { assertYieldsWereCleared(SchedulerMock); // Create the error object before doing any async work, to get a better @@ -123,8 +123,14 @@ export async function waitForThrow(expectedError: mixed) { try { SchedulerMock.unstable_flushAllWithoutAsserting(); } catch (x) { + if (expectedError === undefined) { + // If no expected error was provided, then assume the caller is OK with + // any error being thrown. We're returning the error so they can do + // their own checks, if they wish. + return x; + } if (equals(x, expectedError)) { - return; + return x; } if ( typeof expectedError === 'string' && @@ -133,7 +139,7 @@ export async function waitForThrow(expectedError: mixed) { typeof x.message === 'string' && x.message.includes(expectedError) ) { - return; + return x; } error.message = ` Expected error was not thrown. diff --git a/packages/react-dom/src/__tests__/ReactDOMFloat-test.js b/packages/react-dom/src/__tests__/ReactDOMFloat-test.js index 773944db3d014..2df936507724b 100644 --- a/packages/react-dom/src/__tests__/ReactDOMFloat-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMFloat-test.js @@ -17,7 +17,6 @@ import { let JSDOM; let Stream; -let Scheduler; let React; let ReactDOM; let ReactDOMClient; @@ -34,7 +33,7 @@ let hasErrored = false; let fatalError = undefined; let renderOptions; let waitForAll; -let assertLog; +let waitForThrow; function resetJSDOM(markup) { // Test Environment @@ -57,7 +56,6 @@ describe('ReactDOMFloat', () => { beforeEach(() => { jest.resetModules(); JSDOM = require('jsdom').JSDOM; - Scheduler = require('scheduler'); React = require('react'); ReactDOM = require('react-dom'); ReactDOMClient = require('react-dom/client'); @@ -67,7 +65,7 @@ describe('ReactDOMFloat', () => { const InternalTestUtils = require('internal-test-utils'); waitForAll = InternalTestUtils.waitForAll; - assertLog = InternalTestUtils.assertLog; + waitForThrow = InternalTestUtils.waitForThrow; textCache = new Map(); @@ -261,23 +259,6 @@ describe('ReactDOMFloat', () => { ); } - function renderSafelyAndExpect(root, children) { - root.render(children); - return expect(() => { - try { - // TODO: Migrate this to waitForAll() - Scheduler.unstable_flushAll(); - assertLog([]); - } catch (e) { - try { - // TODO: Migrate this to waitForAll() - Scheduler.unstable_flushAll(); - assertLog([]); - } catch (f) {} - } - }); - } - // @gate enableFloat it('can render resources before singletons', async () => { const root = ReactDOMClient.createRoot(document); @@ -382,15 +363,24 @@ describe('ReactDOMFloat', () => { it('warns if you render resource-like elements above or ', async () => { const root = ReactDOMClient.createRoot(document); - renderSafelyAndExpect( - root, - <> - - - foo - - , - ).toErrorDev( + await expect(async () => { + root.render( + <> + + + foo + + , + ); + const aggregateError = await waitForThrow(); + expect(aggregateError.errors.length).toBe(2); + expect(aggregateError.errors[0].message).toContain( + 'Invalid insertion of NOSCRIPT', + ); + expect(aggregateError.errors[1].message).toContain( + 'The node to be removed is not a child of this node', + ); + }).toErrorDev( [ 'Cannot render