avoid_misused_test_matchers
v0.4.0 Warning Testing Rules
Flags expect() calls where the matcher is incompatible with the actual value’s type. For example, using isNull on a non-nullable type, isEmpty on an int, or isList on a String. Misused matchers can cause tests to always pass (hiding bugs) or always fail (making assertions useless).
Why use this rule
Section titled “Why use this rule”A mismatched matcher silently undermines your test suite. expect(42, isNull) will always fail because int is non-nullable, while expect(42, isNotNull) will always pass, giving a false sense of coverage. This rule catches these type mismatches at analysis time before they reach CI.
See also: test package - Matchers
expect(someString, isList); // String is not a Listexpect(someNumber, isEmpty); // int has no isEmptyexpect(someNumber, isNull); // int is non-nullableexpect(someNumber, isNotNull); // always true, redundantexpect(someNumber, hasLength(1)); // int has no lengthexpect(someString, isZero); // String is not a numexpect(someNumber, isTrue); // int is not a boolexpect(someString, isMap); // String is not a Mapexpect(true, isNegative); // bool is not a numexpect(someList, isList);expect(<String, int>{}, isMap);expect(nullableValue, isNull);expect(nullableValue, isNotNull);expect(someList, isEmpty);expect(someString, isEmpty);expect(someList, hasLength(3));expect(someNumber, isZero);expect(5, isPositive);expect(true, isTrue);expect(false, isFalse);Configuration
Section titled “Configuration”This rule is in the core preset, so it is on with preset: core,
preset: recommended or preset: opinionated.
To turn it off:
rules: avoid_misused_test_matchers: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_test_matchers— Prefer using a Matcher instead of a literal value in expect().prefer_expect_later— Use ‘expectLater’ instead of ‘expect’ when testing Futures.format_test_name— Hold test descriptions to a house pattern.prefer_correct_test_file_name— Name test files so the runner actually runs them.