1// Copyright 2018 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'package:flutter/foundation.dart'; 6import 'package:stack_trace/stack_trace.dart' as stack_trace; 7import 'package:test_api/test_api.dart' as test_package; 8 9/// Signature for the [reportTestException] callback. 10typedef TestExceptionReporter = void Function(FlutterErrorDetails details, String testDescription); 11 12/// A function that is called by the test framework when an unexpected error 13/// occurred during a test. 14/// 15/// This function is responsible for reporting the error to the user such that 16/// the user can easily diagnose what failed when inspecting the test results. 17/// It is also responsible for reporting the error to the test framework itself 18/// in order to cause the test to fail. 19/// 20/// This function is pluggable to handle the cases where tests are run in 21/// contexts _other_ than via `flutter test`. 22TestExceptionReporter get reportTestException => _reportTestException; 23TestExceptionReporter _reportTestException = _defaultTestExceptionReporter; 24set reportTestException(TestExceptionReporter handler) { 25 assert(handler != null); 26 _reportTestException = handler; 27} 28 29void _defaultTestExceptionReporter(FlutterErrorDetails errorDetails, String testDescription) { 30 FlutterError.dumpErrorToConsole(errorDetails, forceReport: true); 31 // test_package.registerException actually just calls the current zone's error handler (that 32 // is to say, _parentZone's handleUncaughtError function). FakeAsync doesn't add one of those, 33 // but the test package does, that's how the test package tracks errors. So really we could 34 // get the same effect here by calling that error handler directly or indeed just throwing. 35 // However, we call registerException because that's the semantically correct thing... 36 String additional = ''; 37 if (testDescription.isNotEmpty) 38 additional = '\nThe test description was: $testDescription'; 39 test_package.registerException('Test failed. See exception logs above.$additional', _emptyStackTrace); 40} 41 42final StackTrace _emptyStackTrace = stack_trace.Chain(const <stack_trace.Trace>[]); 43