Skip to main content

prefer-test-matchers

added in: 1.6.0
Pro+

Warns when the second argument of an expect or expectLater is not a subclass of a Matcher.

Even though matchers might require more code to write, they produce a more readable message when a test fails.

Example

❌ Bad:

void main() {
final array = [1, 2, 3];

expect(array.length, 1); // LINT: Prefer test matchers instead of literal values.
expectLater(array.length, 1); // LINT: Prefer test matchers instead of literal values.

const value = 'hello';
expect(value is String, isTrue); // LINT: Prefer the 'isA<T>' matcher instead of the 'is T' check.
}

✅ Good:

void main() {
final array = [1, 2, 3];

expect(array, hasLength(1));
expectLater(array, hasLength(1));

const value = 'hello';
expect(value, isA<String>());
}