dart-best-practices
General best practices for Dart development. Covers code style, effective Dart, and language features.
Best practices for using `expect` and `package:matcher`. Focuses on readable assertions, proper matcher selection, and avoiding common pitfalls.
$ npx -y skills add kevmoo/dash_skills --skill dart-matcher-best-practices --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
/dart-matcher-best-practicesContext preview
The summary Claude sees to decide when to auto-load this skill.
Best practices for using `expect` and `package:matcher`. Focuses on readable assertions, proper matcher selection, and avoiding common pitfalls.
name: dart-matcher-best-practices description: |- Best practices for using `expect` and `package:matcher`. Focuses on readable assertions, proper matcher selection, and avoiding common pitfalls. license: Apache-2.0 key_features: - Package matcher assertions - Readable test expectations - Matcher selection & refactoring
Use this skill when:
Do NOT apply this skill or refactor assertions when:
belongs to a non-collection class (e.g. geometric scalar magnitude on a `Vector`, duration on a buffer, or line segment distance). `hasLength(n)` expects an `int` on an `Iterable`, `Map`, or `String`.
property (e.g. `expect(switch.isEnabled, isTrue)`). Do not invent non-existent matchers.
(`check(x)...`). Do not revert `checks` back to `package:matcher`.
(`hasLength`, `isEmpty`, `isNotEmpty`, `contains`, `containsPair`, `isA`, `throwsA`). Abstain and make zero modifications.
To find candidates for improving matcher usage, search for suboptimal patterns:
Search for length checks that should use `hasLength`:
Search for checks on boolean properties that have specific matchers:
Search for manual map lookups instead of `containsPair`:
`expect(map.containsKey(key), true)`.
`const`.
Use `.having()` on `isA<T>()` or other TypeMatchers to check properties.
`(e) => e.message`) instead of generic ones like `p0` to improve readability.
expect(person, isA<Person>()
.having((p) => p.name, 'name', 'Alice')
.having((p) => p.age, 'age', greaterThan(18)));This provides detailed failure messages indicating exactly which property failed.
the test continues.
function throwing is fine with `expect`).
`try { fn(); fail('should throw'); } catch (e) { expect(e, isA<T>()); }`.
// Synchronous:
expect(() => parseData('invalid'), throwsA(isA<FormatException>()));
// Asynchronous:
await expectLater(client.fetch('bad-url'), throwsA(isA<HttpException>()));`throwsStateError`, `throwsRangeError`, `throwsFormatException`, `throwsNoSuchMethodError`, and `throwsUnsupportedError`.
Use `await expectLater(...)` when testing async behavior to ensure proper sequencing.
// GOOD: Waits for future to complete before checking side effects
await expectLater(future, completion(equals(42)));
expect(sideEffectState, equals('done'));
// BAD: Side effect check might run before future completes
expect(future, completion(equals(42)));
expect(sideEffectState, equals('done')); // Race condition!1. **Readable Failures**: Choose matchers that produce clear error messages. 2. **Avoid Manual Logic**: Don't use `if` statements or `for` loops for assertions; let matchers handle it. 3. **Specific Matchers**: Use the most specific matcher available (e.g., `containsPair` for maps instead of checking keys manually).
General best practices for Dart development. Covers code style, effective Dart, and language features.
Best practices for validating Dart documentation comments. Covers using `dart doc` to catch unresolved references and macros.
Guidelines for handling long lines in Dart code to adhere to the 80-column rule. The `lines_longer_than_80_chars` lint.
Guidelines for using modern Dart features (v3.0 - v3.10) such as Records, Extension Types, Class Modifiers, Wildcards, Digit…
Guidelines and best practices for refactoring consecutive prints, single-line string concatenations, and complex output blocks…
Guidelines for maintaining external Dart packages, covering versioning, publishing workflows, and pull request management. Use…