use-test-throws-matchers
Same as use_test_throws_matchers in Dart lints
- Group
- Style
- Path
lint/style/use-test-throws-matchers- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 508 of 512
Prefers throwsA matchers over try { ...; fail(...); } catch (...) {}.
A try statement with a single catch clause, no finally, and a body
that ends in a call to the test framework's fail hand-rolls what
expect(() => ..., throwsA(...)) states directly, and with worse failure
messages. Like the official rule, only the top-level fail declared in
package:test_api/src/frontend/expect.dart counts. Current test_api
releases no longer declare it there (package:test, test_api, and
flutter_test re-export package:matcher's fail), so only code using
such an old test_api is reported; a local or member function named
fail never is. The declaring library is identified through the package
configuration, including when test_api is a dependency.
try {
parse('');
fail('Expected a FormatException'); // BAD
} on FormatException {}
expect(() => parse(''), throwsA(isA<FormatException>())); // GOOD
Invalid
import 'src/frontend/expect.dart';
import 'src/frontend/expect.dart' as expectations;
int parse(String input) => int.parse(input);
void throwsOnEmptyInput() {
try {
parse('');
fail('Expected a FormatException');
} on FormatException {}
}
void catchAll() {
try {
parse('x');
expectations.fail('no throw');
} catch (e) {
print(e);
}
}
void twoCatchClauses() {
try {
parse('');
fail('no throw');
} on FormatException {
// expected
} on ArgumentError {
// also fine
}
}
void localFailShadows() {
void fail(String message) => print(message);
try {
parse('');
fail('no throw');
} catch (_) {}
} parse(''); fail('Expected a FormatException'); parse('x'); expectations.fail('no throw');Valid
import 'package:test/test.dart';
int parse(String input) => int.parse(input);
void main() {
test('uses throwsA', () {
expect(() => parse(''), throwsA(isA<FormatException>()));
});
test('two catch clauses', () {
try {
parse('');
fail('no throw');
} on FormatException {
// expected
} on ArgumentError {
// also fine
}
});
test('has finally', () {
try {
parse('');
fail('no throw');
} catch (_) {
} finally {
print('done');
}
});
test('fail is not last', () {
try {
fail('no throw');
parse('');
} catch (_) {}
});
test('local fail shadows the matcher', () {
void fail(String message) => print(message);
try {
parse('');
fail('no throw');
} catch (_) {}
});
test('try/finally only', () {
try {
parse('');
} finally {
print('done');
}
});
}Configure use-test-throws-matchers
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"use-test-throws-matchers": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/use-test-throws-matchers: <reason>More style rules
- use-to-and-as-if-applicable
use_to_and_as_if_applicableFlags conversion methods not named
to…oras…. - always-declare-return-types
always_declare_return_typesrecommendedFlags functions and methods declared without a return type.
- always-put-control-body-on-new-line
always_put_control_body_on_new_lineFlags a control-flow body that starts on the same line as its control clause.
- always-put-required-named-parameters-first
always_put_required_named_parameters_firstFlags a required named parameter declared after an optional named one.
- always-specify-types
always_specify_typesRequires explicit type annotations everywhere a type could be inferred.
- annotate-overrides
annotate_overridesrecommendedRequires
@overrideon members that override an inherited declaration. - arguments-ordering
Named arguments follow their resolved parameter declaration order by default.
- avoid-abbreviations-in-doc-comments
avoid_abbreviations_in_doc_commentsFlags Latin abbreviations in doc comments.