no_trivial_assertion
Reports an assertion that is always true, whatever the code under test does. Compare with the value you expect.
- Tier
- blocking
- Scope
- Dart
- Fix
- none
What it reports
Section titled “What it reports”An expect, expectLater or assert that holds whatever the code does:
- a value against itself:
expect(total, total),expect(cart.total, equals(cart.total)),expect(1, 1); - a literal against its own matcher:
expect(true, isTrue),expect(null, isNull),expect(0, isNotNull); - a comparison that always holds, against true:
expect(total == total, isTrue); isNotNullon a value whose type cannot hold null:expect(cart.total, isNotNull)wheretotalis anint;- an
assertoftrue, of a value equal to itself, or of literals that fold to true:assert(true),assert(width == width),assert(2 > 1).
It does not report:
- two sides that differ in any token:
expect(total, sum),expect(1, 1.0); - a value against a matcher:
expect(ready, isTrue); - an assertion that always fails:
expect(true, isFalse),assert(false); - a call, object creation or collection literal against itself:
expect(Point(1, 2), Point(1, 2)). Such an assertion can test the code that makes the value.
An assertion that cannot fail checks nothing: a test with only such assertions is green whatever the code does, yet it looks like a real test. An AI coding agent writes expect(true, isTrue) when it has nothing to assert, or to turn a failing test green without finding out why it fails; expect(result, result) is the same in disguise. The rule is blocking because such an assertion claims a check that is not made.
test_requires_assertion is the other half: it reports a test with no assertion at all. A trivial assertion is what gets a test past it.
Example
Section titled “Example”test('adds the tax to the price', () { final total = priceWithTax(100, rate: 0.2);
expect(total, total); expect(true, isTrue);});test('adds the tax to the price', () { final total = priceWithTax(100, rate: 0.2);
expect(total, 120);});Delete an assert(true) left as a placeholder, or replace it with the condition you meant: assert(width >= 0).
Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
assay: rules: no_trivial_assertion: exclude: ["test/fixtures/**"]Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// This test only shows the runner that the suite is wired up.// ignore: assay/no_trivial_assertionexpect(true, isTrue);// ignore_for_file: assay/no_trivial_assertion silences it in a whole file, and // ignore: no_trivial_assertion without the prefix works too. See Suppressions.
Details
Section titled “Details”All cases
What an assertion is
Section titled “What an assertion is”An assertion is a call of expect or expectLater by its bare name with two positional arguments, or an assert, as a statement or in a constructor’s initializers. The matchers are known by name (isTrue, isFalse, isNull, isNotNull, equals), whatever package they come from. The violation covers the call with its arguments, expect(true, isTrue), or the assert from its keyword to its closing parenthesis, assert(true).
The cases in full
Section titled “The cases in full”An expect is reported in three cases:
- A literal or value against itself. Both arguments are the same plain operand:
expect(1, 1),expect('done', 'done'),expect(total, total),expect(cart.total, cart.total). - A literal against its own matcher.
expect(true, isTrue),expect(false, isFalse),expect(null, isNull), and any literal butnullagainstisNotNull:expect(0, isNotNull). - A comparison that always holds, against true. The first argument is
==between two same plain operands, and the second istrueorisTrue:expect(total == total, isTrue),expect(1 == 1, true).
A second argument equals(x) is read as x, so expect(total, equals(total)) is reported too. A named argument such as reason:, or parentheses around an argument, make no difference.
An assert is reported when its condition is the literal true, or == between two same plain operands: assert(true), assert(width == width). A message as its second argument makes no difference.
A plain operand is a literal (a number, a string without interpolation, true, false, null) or a chain of names (total, cart.total, this.total). Two are the same when written with the same tokens; whitespace makes no difference.
A chain of names is taken to give the same value twice, so a getter that gives another value at each read, expect(clock.now, clock.now), is reported all the same. So are:
value == valuewhere it is meant to test for NaN: writevalue.isNaNthere;- a value compared with itself where that is the test, such as
expect(money, equals(money))for a reflexive==: compare two values built alike,expect(Money(5), Money(5)), or suppress it with a justification; - two forms that always fail:
expect(double.nan, double.nan), and a matcher against itself,expect(isTrue, isTrue).
Two more shapes are always true, each with a message of its own:
isNotNullon a value whose type cannot hold null:expect(cart.total, isNotNull)wheretotalis anint, aFutureor any other non-nullable type. A value of a nullable type,dynamic,Object?, or a type parameter that may be nullable is not reported.- message: “This assertion is always true: the type of the value cannot hold null, so ‘isNotNull’ never fails.”
- how to fix: “Assert on what the value holds; delete the assertion where only making the value is under test.”
- An
assertof literals only, whose condition is made of number, boolean, string and null literals with the comparison, equality and logical operators and!, and folds totrue:assert(2 > 1),assert(!false),assert('a' != 'b'). A condition that folds tofalse(assert(1 > 2), likeassert(false)) marks a place not to reach, and is not reported. Nor is one that reads a name, even a constant’s:assert(maxItems > 0)checks a setting. The SDK lintliteral_only_boolean_expressionsdoes not look at anassert, so the two do not overlap.- message: “This assertion is always true: its condition is made of literals only, so it holds whatever the code does.”
- how to fix: “Assert a condition on the values the code works with, or delete the assertion.”
What it does not report
Section titled “What it does not report”It does not report:
- two sides that differ in any token:
expect(total, sum),expect('done', "done"),expect(1, 1.0); - a value against a matcher:
expect(ready, isTrue), andexpect(result, isNotNull)whereresultmay be null; - an assertion that always fails:
expect(true, isFalse),assert(false); - a call, object creation, index or collection literal against itself:
expect(cart.sum(), cart.sum()),expect(Point(1, 2), Point(1, 2)),expect([1, 2], [1, 2]). Such an assertion can test the code that makes the value: that a call gives the same answer twice, or that a class has an==.