Skip to content

Commit 623777b

Browse files
authored
test: use proper matchers in tests (#1437)
## What kind of change does this PR introduce? Test quality cleanup. No production code changes; only test files are touched. ## What is the current behavior? Several test suites used non-idiomatic assertions: - boolean-literal expectations, e.g. `expect(x, true)` / `expect(x, false)` - equality checks done inside the actual argument, e.g. `expect(a == b, true)` - length-zero emptiness checks, e.g. `expect(x.length, 0)` - exception assertions written as `try { await ...; fail('...'); } catch (e) { expect(e, ...) }` ## What is the new behavior? These are replaced with the proper matchers across all packages: - `expect(x, isTrue)` / `expect(x, isFalse)` - `expect(a, b)` / `expect(a, isNot(b))` - `expect(x, isEmpty)` - `expectLater(() => ..., throwsA(isA<T>().having((e) => e.field, 'field', value)))` Approximate counts: ~47 boolean matchers, ~22 direct equality, ~8 emptiness, ~43 exception conversions across 22 test files in functions_client, gotrue, postgrest, realtime_client, storage_client, supabase, and supabase_flutter. A few `fail()` calls were intentionally left in place because they are not exception-testing patterns (one sits inside an `await for` stream loop, two assert that a download succeeds after copy/move). ## How was the change verified? - `dart analyze` / `flutter analyze` on every affected package's `test/` directory: no issues. - `dart format` on all changed files: already conformant. - All mock-based suites pass. Server-dependent integration suites were not run locally but analyze cleanly; the conversions are mechanical and preserve assertion meaning.
1 parent 9e71a24 commit 623777b

21 files changed

Lines changed: 345 additions & 422 deletions

packages/functions_client/test/functions_dart_test.dart

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ void main() {
2020
FunctionsClient("", {}, httpClient: customHttpClient);
2121
});
2222
test('function throws', () async {
23-
try {
24-
await functionsCustomHttpClient.invoke('error-function');
25-
fail('should throw');
26-
} on FunctionException catch (e) {
27-
expect(e.status, 420);
28-
}
23+
await expectLater(
24+
() => functionsCustomHttpClient.invoke('error-function'),
25+
throwsA(
26+
isA<FunctionException>().having((e) => e.status, 'status', 420)),
27+
);
2928
});
3029

3130
test('function call', () async {
@@ -411,15 +410,14 @@ void main() {
411410

412411
group('Error handling', () {
413412
test('FunctionException contains all error details', () async {
414-
try {
415-
await functionsCustomHttpClient.invoke('error-function');
416-
fail('should throw');
417-
} on FunctionException catch (e) {
418-
expect(e.status, 420);
419-
expect(e.details, isNotNull);
420-
expect(e.reasonPhrase, isNotNull);
421-
expect(e.toString(), contains('420'));
422-
}
413+
await expectLater(
414+
() => functionsCustomHttpClient.invoke('error-function'),
415+
throwsA(isA<FunctionException>()
416+
.having((e) => e.status, 'status', 420)
417+
.having((e) => e.details, 'details', isNotNull)
418+
.having((e) => e.reasonPhrase, 'reasonPhrase', isNotNull)
419+
.having((e) => e.toString(), 'toString()', contains('420'))),
420+
);
423421
});
424422
});
425423

packages/gotrue/test/client_test.dart

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,11 @@ void main() {
104104
test(
105105
'signUp() with weak password throws AuthWeakPasswordException',
106106
() async {
107-
try {
108-
await client.signUp(email: newEmail, password: '123');
109-
fail('signUp with weak password should throw exception');
110-
} on AuthException catch (error) {
111-
expect(error, isA<AuthWeakPasswordException>());
112-
expect(error.code, ErrorCode.weakPassword.code);
113-
} catch (error) {
114-
fail('signUp threw ${error.runtimeType} instead of AuthException');
115-
}
107+
await expectLater(
108+
() => client.signUp(email: newEmail, password: '123'),
109+
throwsA(isA<AuthWeakPasswordException>()
110+
.having((e) => e.code, 'code', ErrorCode.weakPassword.code)),
111+
);
116112
},
117113
);
118114

@@ -125,10 +121,10 @@ void main() {
125121
final urlWithoutAccessToken = Uri.parse(
126122
'http://my-callback-url.com/welcome#expires_in=$expiresIn&refresh_token=$refreshToken&token_type=$tokenType&provider_token=$providerToken',
127123
);
128-
try {
129-
await client.getSessionFromUrl(urlWithoutAccessToken);
130-
fail('getSessionFromUrl did not throw exception');
131-
} catch (_) {}
124+
await expectLater(
125+
() => client.getSessionFromUrl(urlWithoutAccessToken),
126+
throwsA(anything),
127+
);
132128
});
133129

134130
test('Parsing an error URL should throw', () async {
@@ -138,18 +134,13 @@ void main() {
138134
final urlWithoutAccessToken = Uri.parse(
139135
'http://my-callback-url.com/#error=unauthorized_client&error_code=401&error_description=${Uri.encodeComponent(errorMessage)}',
140136
);
141-
try {
142-
await client.getSessionFromUrl(urlWithoutAccessToken);
143-
fail('getSessionFromUrl did not throw exception');
144-
} on AuthException catch (error) {
145-
expect(error.message, errorMessage);
146-
expect(error.statusCode, '401');
147-
expect(error.code, 'unauthorized_client');
148-
} catch (error) {
149-
fail(
150-
'getSessionFromUrl threw ${error.runtimeType} instead of AuthException',
151-
);
152-
}
137+
await expectLater(
138+
() => client.getSessionFromUrl(urlWithoutAccessToken),
139+
throwsA(isA<AuthException>()
140+
.having((e) => e.message, 'message', errorMessage)
141+
.having((e) => e.statusCode, 'statusCode', '401')
142+
.having((e) => e.code, 'code', 'unauthorized_client')),
143+
);
153144
});
154145

155146
test('Subscribe a listener', () async {
@@ -442,12 +433,11 @@ void main() {
442433

443434
test('Update user with the same password throws AuthException', () async {
444435
await client.signInWithPassword(email: email1, password: password);
445-
try {
446-
await client.updateUser(UserAttributes(password: password));
447-
fail('updateUser did not throw');
448-
} on AuthException catch (error) {
449-
expect(error.code, ErrorCode.samePassword.code);
450-
}
436+
await expectLater(
437+
() => client.updateUser(UserAttributes(password: password)),
438+
throwsA(isA<AuthException>()
439+
.having((e) => e.code, 'code', ErrorCode.samePassword.code)),
440+
);
451441
});
452442

453443
test('signOut', () async {
@@ -473,15 +463,14 @@ void main() {
473463
});
474464

475465
test('signIn() with the wrong password', () async {
476-
try {
477-
await client.signInWithPassword(
466+
await expectLater(
467+
() => client.signInWithPassword(
478468
email: email1,
479469
password: 'wrong_$password',
480-
);
481-
fail('signInWithPassword did not throw');
482-
} on AuthException catch (error) {
483-
expect(error.message, isNotEmpty);
484-
}
470+
),
471+
throwsA(isA<AuthException>()
472+
.having((e) => e.message, 'message', isNotNull)),
473+
);
485474
});
486475

487476
group('The auth client can signin with third-party oAuth providers', () {
@@ -686,16 +675,11 @@ void main() {
686675
final urlWithoutAccessToken = Uri.parse(
687676
'http://my-callback-url.com/#error=unauthorized_client&error_code=401&error_description=${Uri.encodeComponent(errorMessage)}',
688677
);
689-
try {
690-
await client.getSessionFromUrl(urlWithoutAccessToken);
691-
fail('getSessionFromUrl did not throw exception');
692-
} on AuthException catch (error) {
693-
expect(error.message, errorMessage);
694-
} catch (error) {
695-
fail(
696-
'getSessionFromUrl threw ${error.runtimeType} instead of AuthException',
697-
);
698-
}
678+
await expectLater(
679+
() => client.getSessionFromUrl(urlWithoutAccessToken),
680+
throwsA(isA<AuthException>()
681+
.having((e) => e.message, 'message', errorMessage)),
682+
);
699683
});
700684

701685
test(

packages/gotrue/test/fetch_test.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,10 @@ void main() {
7272

7373
Future<void> _testFetchRequest(Client client) async {
7474
final GotrueFetch fetch = GotrueFetch(client);
75-
try {
76-
await fetch.request(_mockUrl, RequestMethodType.get);
77-
} on AuthException catch (error) {
78-
expect(error.code, 'weak_password');
79-
expect(error.message, 'error_message');
80-
} catch (error) {
81-
fail('Should have thrown AuthException');
82-
}
75+
await expectLater(
76+
() => fetch.request(_mockUrl, RequestMethodType.get),
77+
throwsA(isA<AuthException>()
78+
.having((e) => e.code, 'code', 'weak_password')
79+
.having((e) => e.message, 'message', 'error_message')),
80+
);
8381
}

0 commit comments

Comments
 (0)