avoid-top-level-members-in-tests

Path
lint/suspicious/avoid-top-level-members-in-tests
Default
Off, opt in
Origin
Falcon
Domain
test
Rule
147 of 512
test

Requires public declarations in test files, except main, to be private.

Private test helpers and fixtures cannot accidentally become another file's API and can be identified as unused without cross-library consumers.

Invalid

example.dartdart
import 'package:test/test.dart';
var count = 0;
const value = 1;
final values = <int>[];
void helper() {}
class Helper {}
mixin Behavior {}
enum Choice { first }
extension Description on int { String describe() => '$this'; }
typedef Callback = void Function();
void main() { test('case', () {}); }
Make this top-level test declaration private.
1import 'package:test/test.dart';
2var count = 0;
Make this top-level test declaration private.
2var count = 0;
3const value = 1;
Make this top-level test declaration private.
3const value = 1;
4final values = <int>[];
Make this top-level test declaration private.
4final values = <int>[];
5void helper() {}
Make this top-level test declaration private.
5void helper() {}
6class Helper {}
Make this top-level test declaration private.
6class Helper {}
7mixin Behavior {}
Make this top-level test declaration private.
7mixin Behavior {}
8enum Choice { first }
Make this top-level test declaration private.
8enum Choice { first }
9extension Description on int { String describe() => '$this'; }
Make this top-level test declaration private.
9extension Description on int { String describe() => '$this'; }
10typedef Callback = void Function();

Valid

example.dartdart
import 'package:test/test.dart';
var _count = 0;
const _value = 1;
final _values = <int>[];
void _helper() {}
class _Helper { int publicMember = 1; }
mixin _Behavior {}
enum _Choice { first }
extension _Description on int { String describe() => '$this'; }
extension on String { int get lengthTwice => length * 2; }
typedef _Callback = void Function();
void main() { var local = 0; test('case', () { local++; }); }

Configure avoid-top-level-members-in-tests

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "avoid-top-level-members-in-tests": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/suspicious/avoid-top-level-members-in-tests: <reason>
  • avoid-types-as-parameter-namesavoid_types_as_parameter_namesrecommended

    Flags implicit formal parameters, catch variables, and type parameters that shadow a type in their enclosing scope.

  • avoid-unrelated-type-assertionsavoid-unrelated-type-assertionsrecommended

    Flags an is check proven impossible by canonical resolved types.

  • banned-usage

    Ban resolved API references, not identifiers that merely share their spelling.

  • cascade-after-null-coalescing

    Flags a cascade after an unparenthesized null-coalescing expression.

  • control-flow-in-finallycontrol_flow_in_finallyrecommended

    Flags return, break, or continue that escapes a finally block.

  • empty-catchesempty_catchesrecommended

    Flags empty catch blocks.

  • empty-statementsempty_statementsrecommended

    Flags stray empty statements (a lone ;).

  • no-duplicate-case-valuesno_duplicate_case_valuesrecommended

    Flags semantically equal constant values within one switch.