Skip to content

package:checks: Add prints expectation for checking captured output #2742

Description

@kevmoo

In package:matcher, prints(...) captures print statements during callback execution via ZoneSpecification(print: ...) and validates the printed output against a string or matcher:

expect(() => print("hello"), prints("hello\n"));

In package:checks, there is currently no direct replacement for prints. The migration document (pkgs/checks/doc/migrating_from_matcher.md) lists:

- prints: TODO add missing expectation? Is this one worth replacing?

Migrating tests that assert on console or logging output requires either maintaining a dependency on package:matcher / package:test/expect.dart or writing custom runZoned capture boilerplate.

Proposed Solution

Add a prints expectation extension on Subject<void Function()> (and async callback subjects) that executes the callback in a Zone intercepting print(), collects the output, and provides a Subject<String> or accepts a condition on the captured output.

Example usage:

check(() {
  print("hello");
}).prints(it()..equals("hello\n"));

// Or with string containment / pattern matching:
check(() {
  print("foo");
  print("bar");
}).prints(it()..contains("foo"));

Acceptance Criteria

  • Provide a prints expectation on synchronous and asynchronous callback Subjects.
  • Capture all print invocations executed within the callback body using a ZoneSpecification.
  • Provide a nested Subject<String> for the concatenated printed output to allow chaining standard String checks (equals, contains, matchesPattern, etc.).
  • Support both synchronous (void Function()) and asynchronous (FutureOr<void> Function()) callbacks.
  • Update pkgs/checks/doc/migrating_from_matcher.md to document the replacement.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

package:checksIssues related to pkg:checks

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions