Skip to content

Commit 14889c8

Browse files
Filter out ReactDOM.render deprecation logs by default
1 parent 203f36e commit 14889c8

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

lib/src/util/console_log_utils.dart

+26-3
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,22 @@ import 'package:react/react_client/react_interop.dart';
2525

2626
import 'console_log_formatter.dart';
2727

28+
// Filter out ReactDOM.render logs by default to help them from interfering with log assertions
29+
// during the transition to React 18 and createRoot.
30+
// TODO make this configurable so consumers can opt into these warnings; ideally in one place for all of react-dart, RTL, over_react_test
31+
bool _defaultLogFilter(String log) => !log.startsWith('Warning: ReactDOM.render is no longer supported in React 18.');
32+
2833
/// Runs a provided [callback] and afterwards [print]s each log that occurs during the runtime
2934
/// of that function.
3035
///
3136
/// [print] is called in the same Zone as this function was called in, similar to a Stream subscription.
3237
///
3338
/// Can be used to print logs, warnings, or errors based on [configuration].
3439
///
40+
/// If [logFilter] returns false for a given log, [onLog] will not be called.
41+
/// The default filter omits React 18 `ReactDOM.render` deprecation messages.
42+
///
43+
///
3544
/// The function assumes that any `propType` warnings that occur during
3645
/// the function runtime should be captured. Consequently, the `PropType` cache
3746
/// is reset prior to calling the provided callback.
@@ -40,13 +49,14 @@ import 'console_log_formatter.dart';
4049
T printConsoleLogs<T>(
4150
T Function() callback, {
4251
ConsoleConfig configuration = ConsoleConfig.all,
52+
bool Function(String) logFilter = _defaultLogFilter,
4353
}) {
4454
// Collect all the print calls and then print them at the end so that we don't hit
4555
// StreamController state errors or stack overflows caused by calling `print` inside
4656
// a `console.log` call called by print.
4757
final printCalls = <String>[];
4858
try {
49-
return spyOnConsoleLogs(callback, configuration: configuration, onLog: printCalls.add);
59+
return spyOnConsoleLogs(callback, configuration: configuration, onLog: printCalls.add, logFilter: logFilter);
5060
} finally {
5161
printCalls.forEach(print);
5262
}
@@ -59,6 +69,9 @@ T printConsoleLogs<T>(
5969
///
6070
/// Can be used to capture logs, warnings, or errors based on [configuration].
6171
///
72+
/// If [logFilter] returns false for a given log, [onLog] will not be called.
73+
/// The default filter omits React 18 `ReactDOM.render` deprecation messages.
74+
///
6275
/// The function assumes that any `propType` warnings that occur during
6376
/// the function runtime should be captured. Consequently, the `PropType` cache
6477
/// is reset prior to calling the provided callback.
@@ -68,8 +81,9 @@ T spyOnConsoleLogs<T>(
6881
T Function() callback, {
6982
required void Function(String) onLog,
7083
ConsoleConfig configuration = ConsoleConfig.all,
84+
bool Function(String) logFilter = _defaultLogFilter,
7185
}) {
72-
final stopSpying = startSpyingOnConsoleLogs(configuration: configuration, onLog: onLog);
86+
final stopSpying = startSpyingOnConsoleLogs(configuration: configuration, onLog: onLog, logFilter: logFilter);
7387
try {
7488
return callback();
7589
} finally {
@@ -84,6 +98,9 @@ T spyOnConsoleLogs<T>(
8498
///
8599
/// Can be used to capture logs, warnings, or errors based on [configuration].
86100
///
101+
/// If [logFilter] returns false for a given log, [onLog] will not be called.
102+
/// The default filter omits React 18 `ReactDOM.render` deprecation messages.
103+
///
87104
/// The function assumes that any `propType` warnings that occur during
88105
/// the function runtime should be captured. Consequently, the `PropType` cache
89106
/// is reset prior to calling the provided callback.
@@ -92,6 +109,7 @@ T spyOnConsoleLogs<T>(
92109
void Function() startSpyingOnConsoleLogs({
93110
ConsoleConfig configuration = ConsoleConfig.all,
94111
required void Function(String) onLog,
112+
bool Function(String) logFilter = _defaultLogFilter,
95113
}) {
96114
final logTypeToCapture = configuration.logType == 'all' ? ConsoleConfig.types : [configuration.logType];
97115
final consoleRefs = <String, /* JavascriptFunction */ dynamic>{};
@@ -128,7 +146,12 @@ void Function() startSpyingOnConsoleLogs({
128146
// loop when the logType is set to `log`.
129147
final args = [arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10]
130148
..removeWhere((arg) => arg == _undefined);
131-
boundOnLog(format(message, args));
149+
{
150+
final logAsCallbackArg = format(message, args);
151+
if (logFilter(logAsCallbackArg)) {
152+
boundOnLog(logAsCallbackArg);
153+
}
154+
}
132155
_jsFunctionApply(consoleRefs[config] as Object, [message, ...args], thisArg: self);
133156
})
134157
}),

0 commit comments

Comments
 (0)