Skip to content

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).

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 List
expect(someNumber, isEmpty); // int has no isEmpty
expect(someNumber, isNull); // int is non-nullable
expect(someNumber, isNotNull); // always true, redundant
expect(someNumber, hasLength(1)); // int has no length
expect(someString, isZero); // String is not a num
expect(someNumber, isTrue); // int is not a bool
expect(someString, isMap); // String is not a Map
expect(true, isNegative); // bool is not a num
expect(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);

This rule is in the core preset, so it is on with preset: core, preset: recommended or preset: opinionated.

To turn it off:

many_lints.yaml
rules:
avoid_misused_test_matchers: false

To keep the rule on but skip certain paths, use per-rule exclude.