dart-best-practices
General best practices for Dart development. Covers code style, effective Dart, and language features.
Core concepts and best practices for `package:test`. Covers `test`, `group`, lifecycle methods (`setUp`, `tearDown`), and configuration (`dart_test.yaml`).
$ npx -y skills add kevmoo/dash_skills --skill dart-test-fundamentals --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
/dart-test-fundamentalsContext preview
The summary Claude sees to decide when to auto-load this skill.
Core concepts and best practices for `package:test`. Covers `test`, `group`, lifecycle methods (`setUp`, `tearDown`), and configuration (`dart_test.yaml`).
name: dart-test-fundamentals description: |- Core concepts and best practices for `package:test`. Covers `test`, `group`, lifecycle methods (`setUp`, `tearDown`), and configuration (`dart_test.yaml`). license: Apache-2.0 key_features: - Package test core concepts - Test lifecycle (setUp, tearDown) - dart_test.yaml configuration
Use this skill when:
Do NOT apply this skill or refactor existing tests when:
hierarchies in untouched existing tests unless explicitly asked, as this causes unwanted diff churn.
`package:checks` or a specialized testing framework; do not revert tests back to legacy `package:matcher` idioms.
state or resources do not need `group`, `setUp`, or `addTearDown`. Do not add ceremonial wrapper boilerplate.
To find candidates for improving test structure:
Search for tests that use `try-finally` for cleanup instead of `addTearDown`:
a test).
test('description', () {
// assertions
});(e.g., `group('MyClient', ...)`).
`group` call if it's the only one.
didn't create unless explicitly asked to. This can cause a LOT of churn in the DIFF that most engineers won't want!
`'throws StateError'` or `'adds API key to URL'`).
`timeout`, `skip`) immediately after the description string, before the callback closure. This improves readability by keeping the test logic last.
test('description', testOn: 'vm', () {
// assertions
});groups).
**Best Practice:**
`addTearDown` instead of a `try-finally` block.
**Avoid:**
test('can create and delete a file', () {
final file = File('temp.txt');
try {
file.writeAsStringSync('hello');
expect(file.readAsStringSync(), 'hello');
} finally {
if (file.existsSync()) file.deleteSync();
}
});**Prefer:**
test('can create and delete a file', () {
final file = File('temp.txt');
// Register teardown immediately after resource creation intent
addTearDown(() {
if (file.existsSync()) file.deleteSync();
});
file.writeAsStringSync('hello');
expect(file.readAsStringSync(), 'hello');
});The `dart_test.yaml` file configures the test runner. Common configurations include:
Define where tests run (vm, chrome, node).
platforms: - vm - chrome
Categorize tests to run specific subsets.
tags:
integration:
timeout: 2xUsage in code:
@Tags(['integration']) import 'package:test/test.dart';
Running tags: `dart test --tags integration`
Set default timeouts for tests.
timeouts: 2x # Double the default timeout
<!-- BEGIN SPOT: dart-test-hermeticity -->
(the public entrypoint seam) and keep `lib/<pkg>.dart` exports strictly scoped to public consumers.
(`package:<pkg>/src/<subsystem>.dart`)—such as unexported parsers, state machines, data models, or algorithms with simple interfaces and rich internal behavior—to unit-test edge cases directly; test thin single-caller helpers through their owning module's entrypoint.
real dependencies and first-party fakes so tests fail when production contracts break—use `package:test_descriptor` (`d.sandbox`, `d.dir`, `d.file`) or `Directory.systemTemp.createTempSync()` (always registered with `addTearDown(() => tempDir.deleteSync(recursive: true))`) for filesystem I/
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.
Best practices for using `expect` and `package:matcher`. Focuses on readable assertions, proper matcher selection, and avoiding…
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…