diff --git a/packages/react-dom/src/__tests__/ReactDOMInvalidARIAHook-test.js b/packages/react-dom/src/__tests__/ReactDOMInvalidARIAHook-test.js index 6f1012b9a8ee7..725cedab1f15a 100644 --- a/packages/react-dom/src/__tests__/ReactDOMInvalidARIAHook-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMInvalidARIAHook-test.js @@ -14,12 +14,15 @@ describe('ReactDOMInvalidARIAHook', () => { let ReactDOMClient; let mountComponent; let act; + let assertConsoleErrorDev; beforeEach(() => { jest.resetModules(); React = require('react'); ReactDOMClient = require('react-dom/client'); act = require('internal-test-utils').act; + assertConsoleErrorDev = + require('internal-test-utils').assertConsoleErrorDev; mountComponent = async function (props) { const container = document.createElement('div'); @@ -35,46 +38,52 @@ describe('ReactDOMInvalidARIAHook', () => { await mountComponent({'aria-label': 'Bumble bees'}); }); it('should warn for one invalid aria-* prop', async () => { - await expect(() => mountComponent({'aria-badprop': 'maybe'})).toErrorDev( + await mountComponent({'aria-badprop': 'maybe'}); + assertConsoleErrorDev([ 'Invalid aria prop `aria-badprop` on
tag. ' + - 'For details, see https://react.dev/link/invalid-aria-props', - ); + 'For details, see https://react.dev/link/invalid-aria-props\n' + + ' in div (at **)', + ]); }); it('should warn for many invalid aria-* props', async () => { - await expect(() => - mountComponent({ - 'aria-badprop': 'Very tall trees', - 'aria-malprop': 'Turbulent seas', - }), - ).toErrorDev( + await mountComponent({ + 'aria-badprop': 'Very tall trees', + 'aria-malprop': 'Turbulent seas', + }); + assertConsoleErrorDev([ 'Invalid aria props `aria-badprop`, `aria-malprop` on
' + - 'tag. For details, see https://react.dev/link/invalid-aria-props', - ); + 'tag. For details, see https://react.dev/link/invalid-aria-props\n' + + ' in div (at **)', + ]); }); it('should warn for an improperly cased aria-* prop', async () => { // The valid attribute name is aria-haspopup. - await expect(() => mountComponent({'aria-hasPopup': 'true'})).toErrorDev( + await mountComponent({'aria-hasPopup': 'true'}); + assertConsoleErrorDev([ 'Unknown ARIA attribute `aria-hasPopup`. ' + - 'Did you mean `aria-haspopup`?', - ); + 'Did you mean `aria-haspopup`?\n' + + ' in div (at **)', + ]); }); it('should warn for use of recognized camel case aria attributes', async () => { // The valid attribute name is aria-haspopup. - await expect(() => mountComponent({ariaHasPopup: 'true'})).toErrorDev( + await mountComponent({ariaHasPopup: 'true'}); + assertConsoleErrorDev([ 'Invalid ARIA attribute `ariaHasPopup`. ' + - 'Did you mean `aria-haspopup`?', - ); + 'Did you mean `aria-haspopup`?\n' + + ' in div (at **)', + ]); }); it('should warn for use of unrecognized camel case aria attributes', async () => { // The valid attribute name is aria-haspopup. - await expect(() => - mountComponent({ariaSomethingInvalid: 'true'}), - ).toErrorDev( + await mountComponent({ariaSomethingInvalid: 'true'}); + assertConsoleErrorDev([ 'Invalid ARIA attribute `ariaSomethingInvalid`. ARIA ' + - 'attributes follow the pattern aria-* and must be lowercase.', - ); + 'attributes follow the pattern aria-* and must be lowercase.\n' + + ' in div (at **)', + ]); }); }); }); diff --git a/packages/react-dom/src/__tests__/ReactDOMLegacyComponentTree-test.internal.js b/packages/react-dom/src/__tests__/ReactDOMLegacyComponentTree-test.internal.js index 8465e68703961..e1ba0415767d4 100644 --- a/packages/react-dom/src/__tests__/ReactDOMLegacyComponentTree-test.internal.js +++ b/packages/react-dom/src/__tests__/ReactDOMLegacyComponentTree-test.internal.js @@ -13,10 +13,13 @@ describe('ReactDOMComponentTree', () => { let React; let ReactDOM; let container; + let assertConsoleErrorDev; beforeEach(() => { React = require('react'); ReactDOM = require('react-dom'); + assertConsoleErrorDev = + require('internal-test-utils').assertConsoleErrorDev; container = document.createElement('div'); document.body.appendChild(container); @@ -31,11 +34,14 @@ describe('ReactDOMComponentTree', () => { it('finds instance of node that is attempted to be unmounted', () => { const component =
; const node = ReactDOM.render(
{component}
, container); - expect(() => ReactDOM.unmountComponentAtNode(node)).toErrorDev( - "unmountComponentAtNode(): The node you're attempting to unmount " + - 'was rendered by React and is not a top-level container. You may ' + - 'have accidentally passed in a React root node instead of its ' + - 'container.', + ReactDOM.unmountComponentAtNode(node); + assertConsoleErrorDev( + [ + "unmountComponentAtNode(): The node you're attempting to unmount " + + 'was rendered by React and is not a top-level container. You may ' + + 'have accidentally passed in a React root node instead of its ' + + 'container.', + ], {withoutStack: true}, ); }); @@ -49,11 +55,14 @@ describe('ReactDOMComponentTree', () => { ); const anotherComponent =
; const instance = ReactDOM.render(component, container); - expect(() => ReactDOM.render(anotherComponent, instance)).toErrorDev( - 'Replacing React-rendered children with a new root ' + - 'component. If you intended to update the children of this node, ' + - 'you should instead have the existing children update their state ' + - 'and render the new components instead of calling ReactDOM.render.', + ReactDOM.render(anotherComponent, instance); + assertConsoleErrorDev( + [ + 'Replacing React-rendered children with a new root ' + + 'component. If you intended to update the children of this node, ' + + 'you should instead have the existing children update their state ' + + 'and render the new components instead of calling ReactDOM.render.', + ], {withoutStack: true}, ); }); diff --git a/packages/react-dom/src/__tests__/ReactDOMLegacyFiber-test.js b/packages/react-dom/src/__tests__/ReactDOMLegacyFiber-test.js index c799e3b904b86..9c1e7f030e1fd 100644 --- a/packages/react-dom/src/__tests__/ReactDOMLegacyFiber-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMLegacyFiber-test.js @@ -13,11 +13,14 @@ const React = require('react'); const ReactDOM = require('react-dom'); const PropTypes = require('prop-types'); let act; +let assertConsoleErrorDev; describe('ReactDOMLegacyFiber', () => { let container; beforeEach(() => { act = require('internal-test-utils').act; + assertConsoleErrorDev = + require('internal-test-utils').assertConsoleErrorDev; container = document.createElement('div'); document.body.appendChild(container); }); @@ -786,9 +789,8 @@ describe('ReactDOMLegacyFiber', () => { } } - expect(() => { - ReactDOM.render(, container); - }).toErrorDev([ + ReactDOM.render(, container); + assertConsoleErrorDev([ 'Parent uses the legacy childContextTypes API which will soon be removed. Use React.createContext() instead.', 'Component uses the legacy contextTypes API which will soon be removed. Use React.createContext() with static contextType instead.', ]); @@ -834,10 +836,8 @@ describe('ReactDOMLegacyFiber', () => { } } - let instance; - expect(() => { - instance = ReactDOM.render(, container); - }).toErrorDev([ + const instance = ReactDOM.render(, container); + assertConsoleErrorDev([ 'Parent uses the legacy childContextTypes API which will soon be removed. Use React.createContext() instead.', 'Component uses the legacy contextTypes API which will soon be removed. Use React.createContext() with static contextType instead.', ]); @@ -882,9 +882,8 @@ describe('ReactDOMLegacyFiber', () => { } } - expect(() => { - ReactDOM.render(, container); - }).toErrorDev([ + ReactDOM.render(, container); + assertConsoleErrorDev([ 'Parent uses the legacy childContextTypes API which will soon be removed. Use React.createContext() instead.', 'Component uses the legacy contextTypes API which will soon be removed. Use React.createContext() with static contextType instead.', ]); @@ -1117,11 +1116,12 @@ describe('ReactDOMLegacyFiber', () => { return
; } } - expect(() => ReactDOM.render(, container)).toErrorDev( + ReactDOM.render(, container); + assertConsoleErrorDev([ 'Expected `onClick` listener to be a function, instead got a value of `string` type.\n' + ' in div (at **)\n' + ' in Example (at **)', - ); + ]); }); // @gate !disableLegacyMode @@ -1131,13 +1131,14 @@ describe('ReactDOMLegacyFiber', () => { return
; } } - expect(() => ReactDOM.render(, container)).toErrorDev( + ReactDOM.render(, container); + assertConsoleErrorDev([ 'Expected `onClick` listener to be a function, instead got `false`.\n\n' + 'If you used to conditionally omit it with onClick={condition && value}, ' + 'pass onClick={condition ? value : undefined} instead.\n' + ' in div (at **)\n' + ' in Example (at **)', - ); + ]); }); // @gate !disableLegacyMode @@ -1270,17 +1271,18 @@ describe('ReactDOMLegacyFiber', () => { container.innerHTML = '
MEOW.
'; await expect(async () => { - await expect(async () => { - await act(() => { - ReactDOM.render(
baz
, container); - }); - }).rejects.toThrow('The node to be removed is not a child of this node.'); - }).toErrorDev( - '' + - 'It looks like the React-rendered content of this container was ' + - 'removed without using React. This is not supported and will ' + - 'cause errors. Instead, call ReactDOM.unmountComponentAtNode ' + - 'to empty a container.', + await act(() => { + ReactDOM.render(
baz
, container); + }); + }).rejects.toThrow('The node to be removed is not a child of this node.'); + assertConsoleErrorDev( + [ + '' + + 'It looks like the React-rendered content of this container was ' + + 'removed without using React. This is not supported and will ' + + 'cause errors. Instead, call ReactDOM.unmountComponentAtNode ' + + 'to empty a container.', + ], {withoutStack: true}, ); }); @@ -1293,12 +1295,15 @@ describe('ReactDOMLegacyFiber', () => { expect(container.innerHTML).toBe('
bar
'); // then we mess with the DOM before an update container.innerHTML = '
MEOW.
'; - expect(() => ReactDOM.render(
baz
, container)).toErrorDev( - '' + - 'It looks like the React-rendered content of this container was ' + - 'removed without using React. This is not supported and will ' + - 'cause errors. Instead, call ReactDOM.unmountComponentAtNode ' + - 'to empty a container.', + ReactDOM.render(
baz
, container); + assertConsoleErrorDev( + [ + '' + + 'It looks like the React-rendered content of this container was ' + + 'removed without using React. This is not supported and will ' + + 'cause errors. Instead, call ReactDOM.unmountComponentAtNode ' + + 'to empty a container.', + ], {withoutStack: true}, ); }); @@ -1311,12 +1316,15 @@ describe('ReactDOMLegacyFiber', () => { expect(container.innerHTML).toBe('
bar
'); // then we mess with the DOM before an update container.innerHTML = ''; - expect(() => ReactDOM.render(
baz
, container)).toErrorDev( - '' + - 'It looks like the React-rendered content of this container was ' + - 'removed without using React. This is not supported and will ' + - 'cause errors. Instead, call ReactDOM.unmountComponentAtNode ' + - 'to empty a container.', + ReactDOM.render(
baz
, container); + assertConsoleErrorDev( + [ + '' + + 'It looks like the React-rendered content of this container was ' + + 'removed without using React. This is not supported and will ' + + 'cause errors. Instead, call ReactDOM.unmountComponentAtNode ' + + 'to empty a container.', + ], {withoutStack: true}, ); }); diff --git a/packages/react-dom/src/__tests__/ReactDOMOption-test.js b/packages/react-dom/src/__tests__/ReactDOMOption-test.js index ce5e3c65bcfdb..cb4270cd1ea20 100644 --- a/packages/react-dom/src/__tests__/ReactDOMOption-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMOption-test.js @@ -14,6 +14,7 @@ describe('ReactDOMOption', () => { let ReactDOMClient; let ReactDOMServer; let act; + let assertConsoleErrorDev; beforeEach(() => { jest.resetModules(); @@ -21,6 +22,8 @@ describe('ReactDOMOption', () => { ReactDOMClient = require('react-dom/client'); ReactDOMServer = require('react-dom/server'); act = require('internal-test-utils').act; + assertConsoleErrorDev = + require('internal-test-utils').assertConsoleErrorDev; }); async function renderIntoDocument(children) { @@ -47,10 +50,8 @@ describe('ReactDOMOption', () => { {1}
{2} ); - let container; - await expect(async () => { - container = await renderIntoDocument(el); - }).toErrorDev( + const container = await renderIntoDocument(el); + assertConsoleErrorDev([ 'In HTML,
cannot be a child of ); - let container; - await expect(async () => { - container = await renderIntoDocument(el); - }).toErrorDev( + const container = await renderIntoDocument(el); + assertConsoleErrorDev([ 'Cannot infer the option value of complex children. ' + - 'Pass a `value` prop or use a plain string as children to