@@ -1978,4 +1978,45 @@ describe('ReactFlight', () => {
19781978 </ div > ,
19791979 ) ;
19801980 } ) ;
1981+
1982+ // @gate enableServerComponentLogs && __DEV__
1983+ it ( 'replays logs, but not onError logs' , async ( ) => {
1984+ function foo ( ) {
1985+ return 'hello' ;
1986+ }
1987+ function ServerComponent ( ) {
1988+ console . log ( 'hi' , { prop : 123 , fn : foo } ) ;
1989+ throw new Error ( 'err' ) ;
1990+ }
1991+
1992+ let transport ;
1993+ expect ( ( ) => {
1994+ // Reset the modules so that we get a new overridden console on top of the
1995+ // one installed by expect. This ensures that we still emit console.error
1996+ // calls.
1997+ jest . resetModules ( ) ;
1998+ jest . mock ( 'react' , ( ) => require ( 'react/react.react-server' ) ) ;
1999+ ReactServer = require ( 'react' ) ;
2000+ ReactNoopFlightServer = require ( 'react-noop-renderer/flight-server' ) ;
2001+ transport = ReactNoopFlightServer . render ( { root : < ServerComponent /> } ) ;
2002+ } ) . toErrorDev ( 'err' ) ;
2003+
2004+ const log = console . log ;
2005+ try {
2006+ console . log = jest . fn ( ) ;
2007+ // The error should not actually get logged because we're not awaiting the root
2008+ // so it's not thrown but the server log also shouldn't be replayed.
2009+ await ReactNoopFlightClient . read ( transport ) ;
2010+
2011+ expect ( console . log ) . toHaveBeenCalledTimes ( 1 ) ;
2012+ expect ( console . log . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'hi' ) ;
2013+ expect ( console . log . mock . calls [ 0 ] [ 1 ] . prop ) . toBe ( 123 ) ;
2014+ const loggedFn = console . log . mock . calls [ 0 ] [ 1 ] . fn ;
2015+ expect ( typeof loggedFn ) . toBe ( 'function' ) ;
2016+ expect ( loggedFn ) . not . toBe ( foo ) ;
2017+ expect ( loggedFn . toString ( ) ) . toBe ( foo . toString ( ) ) ;
2018+ } finally {
2019+ console . log = log ;
2020+ }
2021+ } ) ;
19812022} ) ;
0 commit comments