require_mirror_test
Warns when a library under lib/ has no matching test file:
lib/src/core/version.dart -> test/src/core/version_test.dartlib/parser.dart -> test/parser_test.dartThe message says a test file is missing, not that the code is untested — the rule cannot tell the difference.
It cannot show a test was written first (that needs git history, and is gameable) or that the test exercises anything (that is coverage’s job). What it shows is that no production file shipped without somebody creating a place for its tests, which is the half that matters in practice: the common failure is not a weak test but no test at all.
This rule is in no preset. Whether a project mirrors its test tree is a project decision, so it must be enabled by name.
many_lints: rules: require_mirror_test: truerules: require_mirror_test: true// lib/src/core/version.dart — with no test/src/core/version_test.dartclass Version { const Version(this.major, this.minor);
final int major; final int minor;}class Version { const Version(this.major, this.minor);
final int major; final int minor;}void main() { test('parses a two-part version', () { expect(Version.parse('1.2'), equals(const Version(1, 2))); });}Examples
Section titled “Examples”Defaults: test/, _test.dart
Section titled “Defaults: test/, _test.dart”require_mirror_test: truelib/parser.dart -> test/parser_test.dartlib/src/core/version.dart -> test/src/core/version_test.dartlib/src/net/http_client.dart -> test/src/net/http_client_test.dartA project whose tests live elsewhere
Section titled “A project whose tests live elsewhere”Both the directory and the suffix are configurable — a package using spec/
and _spec.dart names them:
many_lints: rules: require_mirror_test: test_dir: spec suffix: _specrules: require_mirror_test: test_dir: spec suffix: _speclib/src/core/version.dart -> spec/src/core/version_spec.dartAccepting a test that moved
Section titled “Accepting a test that moved”By default a file of the right name satisfies the rule wherever it sits under the test directory, so reorganising the test tree does not light up the whole package:
fallback_anywhere: true # the defaultlib/src/core/version.dart satisfied by test/src/core/version_test.dart (the mirrored path) satisfied by test/unit/version_test.dart (found by the fallback)Set it to false when the exact mirrored path is itself the convention you
want enforced:
many_lints: rules: require_mirror_test: fallback_anywhere: falserules: require_mirror_test: fallback_anywhere: falselib/src/core/version.dart satisfied by test/src/core/version_test.dart NOT satisfied by test/unit/version_test.dart // LINTExempting a layer
Section titled “Exempting a layer”exclude takes globs. Public barrels at the root of lib/, or a directory of
generated adapters, are typical exemptions:
many_lints: rules: require_mirror_test: exclude: - 'lib/*.dart' - 'lib/src/adapters/**'rules: require_mirror_test: exclude: - 'lib/*.dart' - 'lib/src/adapters/**'Files that are never reported
Section titled “Files that are never reported”These need no configuration:
// lib/src/models/user.g.dart — generated, skipped// lib/generated/l10n.dart — under generated/, skipped// lib/src/models/models.dart — a barrel: exports only, so nothing to testexport 'user.dart';export 'order.dart';// lib/src/util/_helpers.dart — nothing public, so no surface a test can targetString _slugify(String value) => value.toLowerCase();Demanding a test for a file of private helpers would only produce empty test files, which is worse than no rule. Barrels are detected from the AST, not the filename, because a barrel is not always named after its directory.
// lib/src/core/version_impl.dart — a part file; its library is checked insteadpart of 'version.dart';A file that both exports and declares is not a barrel — it has code of its own, so it is still reported.
Options
Section titled “Options”| Option | Type | Default | Description |
|---|---|---|---|
test_dir |
string | test |
The directory the mirrored path is rooted at |
suffix |
string | _test |
Appended to the file’s base name |
fallback_anywhere |
bool | true |
Accept a file of the right name anywhere under test_dir |
Known limitations
Section titled “Known limitations”It checks for a file, not for tests. An empty version_test.dart satisfies
the rule. Coverage tooling answers the other half.
Only lib/. Code under bin/, tool/ or example/ is never reported.
Names, not contents, under fallback_anywhere. With the default on, any
version_test.dart anywhere under test/ satisfies every version.dart in the
package, including one in a different directory.
Turning this rule off
Section titled “Turning this rule off”rules: require_mirror_test: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_misused_test_matchers— Detect test matchers used with incompatible value types.format_test_name— Hold test descriptions to a house pattern.prefer_correct_test_file_name— Name test files so the runner actually runs them.prefer_test_matchers— Prefer using a Matcher instead of a literal value in expect().