Repository: flutter
Issue: flutter_test to show async callers in stack traces
Labels: a: tests,framework,f: integration_test,has reproducible steps,P2,found in release: 2.2,team-framework,triaged-framework

I am writing tests for a mobile app.  The same set of calls to `flutter_test` appear many times in the tests, so I factored them out into a function.  Unfortunately, when an `expect` fails inside this function, the resulting error message does not show a full stack trace.  The error message contains only one line, the line inside the function.  The error does not show which line of the test called the function.  This omission occurs only when the function does an `await` before the failing `expect`.

Stack traces are important information for engineers trying to fix failing tests.  Let's change `flutter_test:expect` to show full stack traces.

## Steps to Reproduce

1. Run `flutter create example4`
2. Update the files as follows:

<details><summary>pubspec.yaml</summary>

```yaml
name: example4
description: A new Flutter project.
publish_to: 'none'
version: 1.0.0+1
environment:
  sdk: ">=2.12.0 <3.0.0"
dependencies:
  flutter:
    sdk: flutter
dev_dependencies:
  flutter_test:
    sdk: flutter
  integration_test:
    sdk: flutter
flutter:
```

</details>

<details><summary>integration_test/example_test.dart</summary>

```dart
import 'package:flutter/widgets.dart' show Container;
import 'package:flutter_test/flutter_test.dart' show testWidgets, WidgetTester;
import 'package:integration_test/integration_test.dart'
    show IntegrationTestWidgetsFlutterBinding;

Future<void> throwErr1() async {
  await Future.delayed(Duration(milliseconds: 100));
  throw 'err1';
}

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  testWidgets("test1", (WidgetTester tester) async {
    await tester.pumpWidget(Container());
    await throwErr1();
  });
}
```

</details>

<details><summary>test_driver/integration_test.dart</summary>

```dart
import 'package:integration_test/integration_test_driver.dart'
    show integrationDriver;

Future<void> main() => integrationDriver();
```

</details>

3. Run `flutter drive`

**Expected results:**
A stack trace that includes the caller of the function:
```
When the exception was thrown, this was the stack:
#0      throwErr1 (file:///example4/integration_test/example_test.dart:8:3)
#1      main.<anonymous closure> (file:///example4/integration_test/example_test.dart:15:11)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)
```

**Actual results:**

```
When the exception was thrown, this was the stack:
#0      throwErr1 (file:///example4/integration_test/example_test.dart:8:3)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)
```

<details><summary>flutter drive output</summary>

```
example4 % flutter drive --driver=test_driver/integration_test.dart --target=integration_test/example_test.dart -d test-sim
Running "flutter pub get" in example4...                           692ms
Running Xcode build...                                                  
 └─Compiling, linking and signing...                      2,805ms
Xcode build done.                                           16.9s
VMServiceFlutterDriver: Connecting to Flutter application at http://127.0.0.1:60984/OJZkst1ijsw=/
VMServiceFlutterDriver: Isolate found with number: 119148428024639
VMServiceFlutterDriver: Isolate is paused at start.
VMServiceFlutterDriver: Attempting to resume isolate
flutter: 00:00 +0: test1
VMServiceFlutterDriver: Connected to Flutter application.
flutter: (The following exception is now available via WidgetTester.takeException:)
flutter: ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
flutter: The following message was thrown running a test:
flutter: err1
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      throwErr1 (file:///example4/integration_test/example_test.dart:8:3)
flutter: <asynchronous suspension>
flutter: <asynchronous suspension>
flutter: (elided one frame from package:stack_trace)
flutter: ...
flutter:
flutter: The test description was:
flutter:   test1
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: (If WidgetTester.takeException is called, the above exception will be ignored. If it is not, then the above exception will be dumped when another exception is caught by the framework or when the test ends, whichever happens first, and
then the test will fail due to having not caught or expected the exception.)
flutter: ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
flutter: The following message was thrown running a test:
flutter: err1
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      throwErr1 (file:///example4/integration_test/example_test.dart:8:3)
flutter: <asynchronous suspension>
flutter: <asynchronous suspension>
flutter: (elided one frame from package:stack_trace)
flutter: ...
flutter:
flutter: The test description was:
flutter:   test1
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: 00:00 +0: test1 [E]
flutter:   Test failed. See exception logs above.
  The test description was: test1
flutter:
flutter: 00:00 +0 -1: (tearDownAll)
flutter: Warning: integration_test plugin was not detected.

If you're running the tests with `flutter drive`, please make sure your tests
are in the `integration_test/` directory of your package and use
`flutter test $path_to_test` to run it instead.

If you're running the tests with Android instrumentation or XCTest, this means
that you are not capturing test results properly! See the following link for
how to set up the integration_test plugin:

https://flutter.dev/docs/testing/integration-tests#testing-on-firebase-test-lab
flutter: 00:00 +1 -1: Some tests failed.
Failure Details:
Failure in method: test1
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞═════════════════
The following message was thrown running a test:
err1

When the exception was thrown, this was the stack:
#0      throwErr1 (file:///example4/integration_test/example_test.dart:8:3)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)

The test description was:
  test1
═════════════════════════════════════════════════════════════════

end of failure 1



Failed to stop app
example4 % 
```

</details>

<details><summary>flutter doctor -v output</summary>

```
example4 % flutter doctor -v
[✓] Flutter (Channel stable, 2.2.3, on macOS 11.5.1 20G80 darwin-x64, locale en-US)
    • Flutter version 2.2.3 at /Users/user/.flutter
    • Framework revision f4abaa0735 (8 weeks ago), 2021-07-01 12:46:11 -0700
    • Engine revision 241c87ad80
    • Dart version 2.13.4

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/user/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.5.1, Build version 12E507
    • CocoaPods version 1.10.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (version 2020.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    ✗ Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

[✓] Connected device (3 available)
    • test-sim (mobile) • E1A7325E-D299-42D4-A89C-B5DD7CF7DB06 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
    • iPhone 8 (mobile) • 87606CD6-83B2-433B-9458-FD6944930D2F • ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
    • Chrome (web)      • chrome                               • web-javascript • Google Chrome 92.0.4515.159
    ! Error: User's iPhone is not connected. Xcode will continue when User's iPhone is connected. (code -13)

! Doctor found issues in 1 category.
```

</details>

EDIT: Removed reference to `expect`.  This problem occurs when any exception is thrown.