createListener method

Listener createListener({
  1. required TestRunner testRunner,
  2. required bool isFlutter,
  3. String device = 'all',
  4. bool noIntegration = false,
})

Implementation

Listener createListener({
  required TestRunner testRunner,
  required bool isFlutter,
  String device = 'all',
  bool noIntegration = false,
}) {
  Timer? timer;

  bool canSkip = true;
  bool debounce = false;

  return (WatchEvent event) async {
    if (debounce) {
      return;
    }
    if (!canSkip || await isIgnore(event.path, project)) {
      return;
    }

    debounce = true;
    Timer(const Duration(milliseconds: 300), () {
      debounce = false;
    });

    // Clear the screen before running tests
    printer.println('\x1B[2J');

    canSkip = false;
    timer = Timer(const Duration(seconds: 3), () {
      canSkip = true;
    });

    if (canSkip && testRunner.running) {
      await testRunner.terminate();
      if (timer is Timer) {
        timer!.cancel();
      }
    }

    printer.println('TEST RUN: ${event.type} ${event.path}\n');

    var continueAllTests = true;

    // Skip to running all tests file is removed
    if (event.type != ChangeType.REMOVE) {
      final testFileMatch = await project.findMatchingTest(event.path);
      if (testFileMatch.integrationTest) {
        await prepareAllIntegrationTests(project);
      }

      if (testFileMatch.exists) {
        continueAllTests = await testRunner.run(
          match: testFileMatch,
          noIntegration: noIntegration,
          device: device,
        );
      }
    }

    if (continueAllTests) {
      await testRunner.run(
        noIntegration: noIntegration,
        device: device,
      );
    }
    canSkip = true;
    debounce = false;
    if (timer is Timer) {
      timer!.cancel();
    }
  };
}