@@ -18,13 +18,15 @@ let React;
1818let ReactDOM ;
1919let ReactDOMClient ;
2020let ReactDOMServer ;
21+ let assertConsoleErrorDev ;
2122
2223function initModules ( ) {
2324 jest . resetModules ( ) ;
2425 React = require ( 'react' ) ;
2526 ReactDOM = require ( 'react-dom' ) ;
2627 ReactDOMClient = require ( 'react-dom/client' ) ;
2728 ReactDOMServer = require ( 'react-dom/server' ) ;
29+ assertConsoleErrorDev = require ( 'internal-test-utils' ) . assertConsoleErrorDev ;
2830
2931 // Make them available to the helpers.
3032 return {
@@ -48,6 +50,14 @@ describe('ReactDOMServerIntegration', () => {
4850 resetModules ( ) ;
4951 } ) ;
5052
53+ afterEach ( ( ) => {
54+ // TODO: This is a hack because expectErrors does not restore mock,
55+ // however fixing it requires a major refactor to all these tests.
56+ if ( console . error . mockClear ) {
57+ console . error . mockRestore ( ) ;
58+ }
59+ } ) ;
60+
5161 describe ( 'elements and children' , function ( ) {
5262 function expectNode ( node , type , value ) {
5363 expect ( node ) . not . toBe ( null ) ;
@@ -134,15 +144,15 @@ describe('ReactDOMServerIntegration', () => {
134144 // However this particular warning fires only when creating
135145 // DOM nodes on the client side. We force it to fire early
136146 // so that it gets deduplicated later, and doesn't fail the test.
137- expect ( ( ) => {
138- ReactDOM . flushSync ( ( ) => {
139- const root = ReactDOMClient . createRoot (
140- document . createElement ( 'div' ) ,
141- ) ;
142-
143- root . render ( < nonstandard /> ) ;
144- } ) ;
145- } ) . toErrorDev ( 'The tag <nonstandard> is unrecognized in this browser.' ) ;
147+ ReactDOM . flushSync ( ( ) => {
148+ const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
149+ root . render ( < nonstandard /> ) ;
150+ } ) ;
151+ assertConsoleErrorDev ( [
152+ 'The tag <nonstandard> is unrecognized in this browser. ' +
153+ 'If you meant to render a React component, start its name with an uppercase letter.\n' +
154+ ' in nonstandard (at **)' ,
155+ ] ) ;
146156
147157 const e = await render ( < nonstandard > Text</ nonstandard > ) ;
148158 expect ( e . tagName ) . toBe ( 'NONSTANDARD' ) ;
@@ -984,16 +994,17 @@ describe('ReactDOMServerIntegration', () => {
984994 'object' ,
985995 async render => {
986996 let EmptyComponent = { } ;
987- expect ( ( ) => {
988- EmptyComponent = < EmptyComponent /> ;
989- } ) . toErrorDev (
997+ EmptyComponent = < EmptyComponent /> ;
998+ assertConsoleErrorDev (
990999 gate ( flags => flags . enableOwnerStacks )
9911000 ? [ ]
992- : 'React.jsx: type is invalid -- expected a string ' +
993- '(for built-in components) or a class/function (for composite ' +
994- 'components) but got: object. You likely forgot to export your ' +
995- "component from the file it's defined in, or you might have mixed up " +
996- 'default and named imports.' ,
1001+ : [
1002+ 'React.jsx: type is invalid -- expected a string ' +
1003+ '(for built-in components) or a class/function (for composite ' +
1004+ 'components) but got: object. You likely forgot to export your ' +
1005+ "component from the file it's defined in, or you might have mixed up " +
1006+ 'default and named imports.' ,
1007+ ] ,
9971008 { withoutStack : true } ,
9981009 ) ;
9991010 await render ( EmptyComponent ) ;
@@ -1010,14 +1021,15 @@ describe('ReactDOMServerIntegration', () => {
10101021 'null' ,
10111022 async render => {
10121023 let NullComponent = null ;
1013- expect ( ( ) => {
1014- NullComponent = < NullComponent /> ;
1015- } ) . toErrorDev (
1024+ NullComponent = < NullComponent /> ;
1025+ assertConsoleErrorDev (
10161026 gate ( flags => flags . enableOwnerStacks )
10171027 ? [ ]
1018- : 'React.jsx: type is invalid -- expected a string ' +
1019- '(for built-in components) or a class/function (for composite ' +
1020- 'components) but got: null.' ,
1028+ : [
1029+ 'React.jsx: type is invalid -- expected a string ' +
1030+ '(for built-in components) or a class/function (for composite ' +
1031+ 'components) but got: null.' ,
1032+ ] ,
10211033 { withoutStack : true } ,
10221034 ) ;
10231035 await render ( NullComponent ) ;
@@ -1030,16 +1042,17 @@ describe('ReactDOMServerIntegration', () => {
10301042 'undefined' ,
10311043 async render => {
10321044 let UndefinedComponent = undefined ;
1033- expect ( ( ) => {
1034- UndefinedComponent = < UndefinedComponent /> ;
1035- } ) . toErrorDev (
1045+ UndefinedComponent = < UndefinedComponent /> ;
1046+ assertConsoleErrorDev (
10361047 gate ( flags => flags . enableOwnerStacks )
10371048 ? [ ]
1038- : 'React.jsx: type is invalid -- expected a string ' +
1039- '(for built-in components) or a class/function (for composite ' +
1040- 'components) but got: undefined. You likely forgot to export your ' +
1041- "component from the file it's defined in, or you might have mixed up " +
1042- 'default and named imports.' ,
1049+ : [
1050+ 'React.jsx: type is invalid -- expected a string ' +
1051+ '(for built-in components) or a class/function (for composite ' +
1052+ 'components) but got: undefined. You likely forgot to export your ' +
1053+ "component from the file it's defined in, or you might have mixed up " +
1054+ 'default and named imports.' ,
1055+ ] ,
10431056 { withoutStack : true } ,
10441057 ) ;
10451058
0 commit comments