Skip to content

@tracked decorator causes coverage issues - including on comment lines #418

Open

Description

@BillyRayPreachersSon

I've recently found that using the @tracked decorator is causing coverage issues. The odd thing is our project has many files containing @tracked, and yet only one particular file seems to be affected by this issue.

We're using:
"ember-cli": "3.24.0"
"ember-source": "3.24.7"
"ember-cli-code-coverage": "1.0.3"
"istanbul": "0.4.5"

The issue is that the line after the @tracked property will show as having no coverage. What's ever weirder is that if I /* istanbul ignore next */ the allegedly uncovered line, the ignore comment itself will show as being uncovered - and yet comments cannot have coverage.

Our project is private, so I can't link to it... but I've managed to reproduce the issue on an out-of-the-box 3.24.0 install, with the addition of the ember-cli-code-coverage and istanbul versions mentioned above.

If I generate the following class-backed component, add it to the application.hbs file, and then run COVERAGE=true ember s, opening the coverage report shows the constructor line as not having coverage - even though it has to have run in order for the test to pass.

covered-component.js:

import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';

export default class extends Component {

  @tracked
  foo = null;

  constructor() {
    super(...arguments);

    this.foo = 'bar';
  }

}

covered-component.hbs:

this.foo: {{this.foo}}

covered-component-test.js:

import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';

module('Integration | Component | covered-component', function(hooks) {
  setupRenderingTest(hooks);

  test('it renders', async function(assert) {
    await render(hbs`<CoveredComponent />`);
    assert.equal(this.element.textContent.trim(), 'this.foo: bar');
  });
});

The coverage report for the code above shows this:
image

If I update the constructor line to be ignored...:

/* istanbul ignore next */
constructor() {
  super(...arguments);

  this.foo = 'bar';
}

...re-running the tests and refreshing the coverage report shows the coverage comment is uncovered:
image

The only way I've found to prevent this issue is to put the ignore directive before the @tracked decorator, but we'd rather not do this on our project as it gives a false impression that covered code is not covered.

It's also worth noting that exactly the same behaviour happens if I update to "ember-cli-code-coverage": "2.0.2" - the same pink marks show in the reports in the same places.

Activity

  1. BillyRayPreachersSon commented on Aug 8, 2024

    @BillyRayPreachersSon
    Author

    I should probably add that this doesn't seem to happen with other decorators, e.g. @service doesn't seem to cause the same issue.

  2. BillyRayPreachersSon commented on Sep 9, 2024

    @BillyRayPreachersSon
    Author

    I've removed istanbul, as I realise it wasn't needed (was probably left in our project's package.json from yeas ago).

    I've also tried upgrading to "ember-cli-code-coverage": "3.0.1" but exactly the same behaviour is seen with the coverage failure on the comment line.

  3. BillyRayPreachersSon commented on Sep 12, 2024

    @BillyRayPreachersSon
    Author

    Aaah - I think I've found a workaround.

    It looks like the @tracked decorator causes problems only when a default value has been specified. Compare the output from the same file with and without the default value:

    With default Without default
    image image

    So I have a workaround, but it would be nice to be able to specify default values on tracked properties without this failure.

  4. BillyRayPreachersSon commented on Sep 12, 2024

    @BillyRayPreachersSon
    Author

    It's possible the issue only affects tracked properties with default values that are overridden in the constructor (or other method that the constructor calls).

    Certainly all the places I've seen the issue so far match that, and we have plenty of tracked properties with default values that do not cause this issue.

  5. cah-brian-gantzler commented on Sep 12, 2024

    @cah-brian-gantzler

    I would say this is not really a work around but a code smell. Tools like sonar would report this as "null assigned to the foo is never used". It understands that assigning the value as part of the constructor makes that value the actual default. Assigning a previous value for a default is irrelevant.

    If the value assigned in the constructor is a complex test, then yea you can have it in the constructor, But you can also do things like this

    @tracked
    foo = this.args.myFoo;
    

    One would think this would be undefined in this case, but if you look at the generated code, I think this actually rewrites it as a constructor assignment.

  6. BillyRayPreachersSon commented on Sep 13, 2024

    @BillyRayPreachersSon
    Author

    Even if the coverage is correct, and it's pointing out an unused default value, surely the pink error highlight should be on the correct line and not a few lines down on a comment or other method that definitely has coverage?

    I suspect if the pink highlight was over the = null code in the coverage report, I might have realised a lot sooner what was happening 🙂 .

  7. kategengler commented on Sep 13, 2024

    @kategengler
    Collaborator

    @BillyRayPreachersSon Does this happen with other decorators or only @tracked?

  8. BillyRayPreachersSon commented on Sep 13, 2024

    @BillyRayPreachersSon
    Author

    @kategengler I've only seen this issue when using @tracked, when there's a default value, and when that default value is overridden in the constructor.

  9. BillyRayPreachersSon commented on Feb 10, 2025

    @BillyRayPreachersSon
    Author

    @kategengler - I've just seen this with another decorator (@action). In this case, the willRemove action was never called, and yet the uncovered line is shown to be line 72 - the blank line after the method in question.

    it also looks like covered lines also seem to slip by two lines after starting to use @action, as the green mark against line 77 should probably be against line 75, and those at lines 82-84 should probably be against lines 80-82.

    Image
  10. cah-brian-gantzler commented on Feb 10, 2025

    @cah-brian-gantzler

    What is before line 61? because I am thinking the 26X on line 67 was really meant for line 65. Appears the whole file is off by two lines and it stared much higher.

  11. BillyRayPreachersSon commented on Feb 11, 2025

    @BillyRayPreachersSon
    Author

    What is before line 61? because I am thinking the 26X on line 67 was really meant for line 65. Appears the whole file is off by two lines and it stared much higher.

    Some service injections, getters, and an ember-concurrency task:

    Image

  12. cah-brian-gantzler commented on Feb 11, 2025

    @cah-brian-gantzler

    Cant explain line 2, where was that 9x supposed to be, but looks like every line is off by 2. Dont know what the issue is just pointing that out. Hoping someone knows how the map works that maps counts to lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions