Skip to content

Mixin coverage incorrectly reported when mixin is only applied in tests #51784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jellynoone opened this issue Mar 20, 2023 · 2 comments
Open
Labels
area-test Cross-cutting test issues (use area- labels for specific failures; not used for package:test).

Comments

@jellynoone
Copy link

jellynoone commented Mar 20, 2023

General description:
When a mixin is provided by a library but only applied in tests, coverage isn't reported for these mixins.

Meaning, a mixin only has its coverage reported if it is applied in the package and then the class applying it used in tests.

Consider the following example files:

# pubspec.yaml
name: _coverage_test
publish_to: none

environment:
  sdk: '>=2.19.4 <3.0.0'

dev_dependencies:
  test: ^1.23.1
// lib/lib.dart
library _coverage_test;

mixin CalculatorMixin {
  int calculate() => 10;
}

abstract class CalculatorBase {
  int calculate() => 10;
}

class AppliedCalculatorMixin with CalculatorMixin {}

class AppliedCalculatorBase with CalculatorBase {}

and

// test/lib_test.dart
import 'package:_coverage_test/lib.dart';
import 'package:test/test.dart';

void main() {
  group('$CalculatorMixin', () {
    test('calculate()', () {
      expect(TestAppliedCalculatorMixin().calculate(), 10);
      // expect(AppliedCalculatorMixin().calculate(), 10);
    });
  });
  group('$CalculatorBase', () {
    test('calculate()', () {
      expect(TestAppliedCalculatorBase().calculate(), 10);
      // expect(AppliedCalculatorBase().calculate(), 10);
    });
  });
}

class TestAppliedCalculatorMixin with CalculatorMixin {}

class TestAppliedCalculatorBase with CalculatorBase {}

If we run:

rm coverage/**/*.* ; \
dart pub global run coverage:test_with_coverage -o coverage && \
dart pub global run coverage:format_coverage --lcov --check-ignore --in=coverage --out=coverage/coverage.lcov --report-on=lib && \
genhtml coverage/coverage.lcov -o coverage/html

coverage is reported as: lines......: 0.0% (0 of 2 lines)

If we, however, uncomment:

  • expect(AppliedCalculatorMixin().calculate(), 10); and,
  • expect(AppliedCalculatorBase().calculate(), 10);
    and re-run the previous command, coverage is reported as being 100% (as expected).

Expected behaviour
Coverage should be correctly reported for mixins even when they are only applied in test files.

Additional note
Implementing TestAppliedCalculatorBase as class TestAppliedCalculatorBase extends CalculatorBase {} reports correct coverage for CalculatorBase even when only used in tests.

dart --version: Dart SDK version: 2.19.4 (stable) (Tue Mar 7 09:58:46 2023 +0000) on "macos_x64"

@stevemessick stevemessick added the area-test Cross-cutting test issues (use area- labels for specific failures; not used for package:test). label Mar 20, 2023
@jellynoone
Copy link
Author

Further tested this with:
Dart SDK version: 3.0.0-290.3.beta (beta) (Tue Mar 21 16:51:50 2023 +0000) on "macos_x64" and it is working as expected i.e. the line coverage is correctly reported.

On the other hand, I tested this with:
Dart SDK version: 2.19.5 (stable) (Mon Mar 20 17:09:37 2023 +0000) on "macos_x64" and the line coverage is not correctly reported.

Additionally, it seems that this issue is the same as one reported at #49887.

cc @liamappelbe as you seemed to have resolved the previous one.

@liamappelbe
Copy link
Contributor

That's odd. Seems like it could be the same bug as #49887, but the fix for that landed in 2.18.3. It's good that this new bug seems to be fixed in 3.0.0, but I'd like to understand why it's happening in 2.19.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-test Cross-cutting test issues (use area- labels for specific failures; not used for package:test).
Projects
None yet
Development

No branches or pull requests

3 participants