@@ -25,13 +25,22 @@ import 'package:react/react_client/react_interop.dart';
25
25
26
26
import 'console_log_formatter.dart' ;
27
27
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
+
28
33
/// Runs a provided [callback] and afterwards [print] s each log that occurs during the runtime
29
34
/// of that function.
30
35
///
31
36
/// [print] is called in the same Zone as this function was called in, similar to a Stream subscription.
32
37
///
33
38
/// Can be used to print logs, warnings, or errors based on [configuration] .
34
39
///
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
+ ///
35
44
/// The function assumes that any `propType` warnings that occur during
36
45
/// the function runtime should be captured. Consequently, the `PropType` cache
37
46
/// is reset prior to calling the provided callback.
@@ -40,13 +49,14 @@ import 'console_log_formatter.dart';
40
49
T printConsoleLogs <T >(
41
50
T Function () callback, {
42
51
ConsoleConfig configuration = ConsoleConfig .all,
52
+ bool Function (String ) logFilter = _defaultLogFilter,
43
53
}) {
44
54
// Collect all the print calls and then print them at the end so that we don't hit
45
55
// StreamController state errors or stack overflows caused by calling `print` inside
46
56
// a `console.log` call called by print.
47
57
final printCalls = < String > [];
48
58
try {
49
- return spyOnConsoleLogs (callback, configuration: configuration, onLog: printCalls.add);
59
+ return spyOnConsoleLogs (callback, configuration: configuration, onLog: printCalls.add, logFilter : logFilter );
50
60
} finally {
51
61
printCalls.forEach (print);
52
62
}
@@ -59,6 +69,9 @@ T printConsoleLogs<T>(
59
69
///
60
70
/// Can be used to capture logs, warnings, or errors based on [configuration] .
61
71
///
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
+ ///
62
75
/// The function assumes that any `propType` warnings that occur during
63
76
/// the function runtime should be captured. Consequently, the `PropType` cache
64
77
/// is reset prior to calling the provided callback.
@@ -68,8 +81,9 @@ T spyOnConsoleLogs<T>(
68
81
T Function () callback, {
69
82
required void Function (String ) onLog,
70
83
ConsoleConfig configuration = ConsoleConfig .all,
84
+ bool Function (String ) logFilter = _defaultLogFilter,
71
85
}) {
72
- final stopSpying = startSpyingOnConsoleLogs (configuration: configuration, onLog: onLog);
86
+ final stopSpying = startSpyingOnConsoleLogs (configuration: configuration, onLog: onLog, logFilter : logFilter );
73
87
try {
74
88
return callback ();
75
89
} finally {
@@ -84,6 +98,9 @@ T spyOnConsoleLogs<T>(
84
98
///
85
99
/// Can be used to capture logs, warnings, or errors based on [configuration] .
86
100
///
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
+ ///
87
104
/// The function assumes that any `propType` warnings that occur during
88
105
/// the function runtime should be captured. Consequently, the `PropType` cache
89
106
/// is reset prior to calling the provided callback.
@@ -92,6 +109,7 @@ T spyOnConsoleLogs<T>(
92
109
void Function () startSpyingOnConsoleLogs ({
93
110
ConsoleConfig configuration = ConsoleConfig .all,
94
111
required void Function (String ) onLog,
112
+ bool Function (String ) logFilter = _defaultLogFilter,
95
113
}) {
96
114
final logTypeToCapture = configuration.logType == 'all' ? ConsoleConfig .types : [configuration.logType];
97
115
final consoleRefs = < String , /* JavascriptFunction */ dynamic > {};
@@ -128,7 +146,12 @@ void Function() startSpyingOnConsoleLogs({
128
146
// loop when the logType is set to `log`.
129
147
final args = [arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10]
130
148
..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
+ }
132
155
_jsFunctionApply (consoleRefs[config] as Object , [message, ...args], thisArg: self);
133
156
})
134
157
}),
0 commit comments