Clone this repo:
  1. 72c709a Update CI (#30) by Kevin Moore · 3 months ago master
  2. be93d8f Fix formatting (#29) by Kevin Moore · 4 months ago
  3. 5631a06 Prepare to publish stable null safety (#28) by Nate Bosch · 6 months ago 1.1.0
  4. 7b79e69 Migrate to GitHub Actions (#27) by Alexander Thomas · 7 months ago
  5. 1917666 Merge pull request #26 from dart-lang/pq-patch-1 by Phil Quitslund · 9 months ago

This package provides a Clock class which encapsulates the notion of the “current time” and provides easy access to points relative to the current time. Different Clocks can have a different notion of the current time, and the default top-level clock's notion can be swapped out to reliably test timing-dependent code.

For example, you can use clock in your libraries like this:

// run_with_timing.dart
import 'package:clock/clock.dart';

/// Runs [callback] and prints how long it took.
T runWithTiming<T>(T Function() callback) {
  var stopwatch = clock.stopwatch()..start();
  var result = callback();
  print('It took ${stopwatch.elapsed}!');
  return result;

...and then test your code using the fake_async package, which automatically overrides the current clock:

// run_with_timing_test.dart
import 'run_with_timing.dart';

import 'package:fake_async/fake_async.dart';
import 'package:test/test.dart';

void main() {
  test('runWithTiming() prints the elapsed time', () {
    FakeAsync().run((async) {
      expect(() {
        runWithTiming(() {
          async.elapse(Duration(seconds: 10));
      }, prints('It took 0:00:10.000000!'));