Skip to content

Commit 2149060

Browse files
chore: ignore errors that are not the result of Amplify.configure() (#4835)
1 parent dc7de26 commit 2149060

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

packages/analytics/amplify_analytics_pinpoint/example/integration_test/no_unauth_test.dart

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import 'dart:async';
5+
46
import 'package:amplify_analytics_pinpoint/amplify_analytics_pinpoint.dart';
57
import 'package:amplify_analytics_pinpoint_example/amplifyconfiguration.dart';
68
import 'package:amplify_api/amplify_api.dart';
@@ -34,11 +36,40 @@ void main() {
3436
),
3537
AmplifyAPI(),
3638
]);
37-
await expectLater(
38-
Amplify.configure(amplifyEnvironments[environmentName]!),
39-
completes,
39+
await completesWithoutError(
40+
() => Amplify.configure(amplifyEnvironments[environmentName]!),
4041
);
4142
});
4243
}
4344
});
4445
}
46+
47+
/// Tests that [callback] can complete without an error, while ignoring any errors
48+
/// that are not the direct result of [callback].
49+
///
50+
/// This is useful for testing methods that may initiate other async functions
51+
/// without waiting for them to complete. If async functions that are not awaited
52+
/// on result in an error, they are ignored.
53+
///
54+
/// Once analytics is configured, events will be sent to pinpoint every n seconds.
55+
/// If the user is not signed in and guest access is not enabled, these requests
56+
/// will fail until the user logs in. These failures are expected and should not
57+
/// fail the test.
58+
Future<void> completesWithoutError(Future<Object?> Function() callback) async {
59+
await runZonedGuarded(
60+
() async {
61+
await callback().onError(
62+
(error, stackTrace) {
63+
fail('should complete without error but failed with $error');
64+
},
65+
);
66+
},
67+
(error, stack) {
68+
if (error is TestFailure) {
69+
fail(error.message ?? 'should complete without error');
70+
} else {
71+
// ignore errors that do not arise from the provided callback
72+
}
73+
},
74+
);
75+
}

0 commit comments

Comments
 (0)