@@ -1783,6 +1783,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
17831783 );
17841784 }
17851785
1786+ _currentTestDescription = description;
17861787 _oldExceptionHandler = FlutterError .onError;
17871788 _oldStackTraceDemangler = FlutterError .demangleStackTrace;
17881789 var exceptionCount = 0 ; // number of un-taken exceptions
@@ -1989,6 +1990,9 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
19891990 asyncBarrier (); // When using AutomatedTestWidgetsFlutterBinding, this flushes the microtasks.
19901991 }
19911992
1993+ // The description of the currently running test, saved in [_runTest] so that
1994+ // [postTest] can include it when reporting invariant failures.
1995+ String _currentTestDescription = '' ;
19921996 bool _beforeTestAutoUpdateGoldens = false ;
19931997 late TestExceptionReporter _beforeTestReportTestException;
19941998 late ErrorWidgetBuilder _beforeTestErrorWidgetBuilder;
@@ -2087,7 +2091,13 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
20872091 void _verifyAutoUpdateGoldensUnset (bool valueBeforeTest) {
20882092 assert (() {
20892093 if (autoUpdateGoldenFiles != valueBeforeTest) {
2090- throw FlutterError ('The value of autoUpdateGoldenFiles was changed by the test.' );
2094+ FlutterError .reportError (
2095+ FlutterErrorDetails (
2096+ exception: FlutterError ('The value of autoUpdateGoldenFiles was changed by the test.' ),
2097+ stack: StackTrace .current,
2098+ library: 'Flutter test framework' ,
2099+ ),
2100+ );
20912101 }
20922102 return true ;
20932103 }());
@@ -2101,7 +2111,13 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
21012111 // So we reset the error reporter to its initial value and then report
21022112 // this error.
21032113 reportTestException = valueBeforeTest;
2104- throw FlutterError ('The value of reportTestException was changed by the test.' );
2114+ FlutterError .reportError (
2115+ FlutterErrorDetails (
2116+ exception: FlutterError ('The value of reportTestException was changed by the test.' ),
2117+ stack: StackTrace .current,
2118+ library: 'Flutter test framework' ,
2119+ ),
2120+ );
21052121 }
21062122 return true ;
21072123 }());
@@ -2110,7 +2126,13 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
21102126 void _verifyErrorWidgetBuilderUnset (ErrorWidgetBuilder valueBeforeTest) {
21112127 assert (() {
21122128 if (ErrorWidget .builder != valueBeforeTest) {
2113- throw FlutterError ('The value of ErrorWidget.builder was changed by the test.' );
2129+ FlutterError .reportError (
2130+ FlutterErrorDetails (
2131+ exception: FlutterError ('The value of ErrorWidget.builder was changed by the test.' ),
2132+ stack: StackTrace .current,
2133+ library: 'Flutter test framework' ,
2134+ ),
2135+ );
21142136 }
21152137 return true ;
21162138 }());
@@ -2119,8 +2141,14 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
21192141 void _verifyShouldPropagateDevicePointerEventsUnset (bool valueBeforeTest) {
21202142 assert (() {
21212143 if (shouldPropagateDevicePointerEvents != valueBeforeTest) {
2122- throw FlutterError (
2123- 'The value of shouldPropagateDevicePointerEvents was changed by the test.' ,
2144+ FlutterError .reportError (
2145+ FlutterErrorDetails (
2146+ exception: FlutterError (
2147+ 'The value of shouldPropagateDevicePointerEvents was changed by the test.' ,
2148+ ),
2149+ stack: StackTrace .current,
2150+ library: 'Flutter test framework' ,
2151+ ),
21242152 );
21252153 }
21262154 return true ;
@@ -2185,7 +2213,25 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
21852213 library: 'Flutter test framework' ,
21862214 );
21872215 }
2216+ // Some invariant checks (such as debugAssertNoTransientCallbacks and the
2217+ // _verify*Unset helpers above) report failures through
2218+ // FlutterError.reportError instead of throwing. During the test,
2219+ // FlutterError.onError stores them in _pendingExceptionDetails, which is
2220+ // normally reported by the test completion handler. That handler has
2221+ // already run by the time postTest is called, so these errors must be
2222+ // picked up here or they would be silently dropped.
2223+ final FlutterErrorDetails ? reportedError = _pendingExceptionDetails;
2224+ if (reportedError != null ) {
2225+ if (invariantError == null ) {
2226+ invariantError = reportedError;
2227+ } else {
2228+ debugPrint = debugPrintOverride; // just in case the test overrides it -- otherwise we won't see the error!
2229+ FlutterError .dumpErrorToConsole (reportedError, forceReport: true );
2230+ }
2231+ }
21882232 }
2233+ final String testDescription = _currentTestDescription;
2234+ _currentTestDescription = '' ;
21892235 FlutterError .onError = _oldExceptionHandler;
21902236 FlutterError .demangleStackTrace = _oldStackTraceDemangler;
21912237 _pendingExceptionDetails = null ;
@@ -2223,7 +2269,8 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
22232269 ServicesBinding .instance.resetInternalState ();
22242270
22252271 if (invariantError != null ) {
2226- reportTestException (invariantError, '' );
2272+ debugPrint = debugPrintOverride; // just in case the test overrides it -- otherwise we won't see the error!
2273+ reportTestException (invariantError, testDescription);
22272274 }
22282275 }
22292276}
0 commit comments