Repository navigation
Description
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:

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:

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
BillyRayPreachersSon commentedon Aug 8, 2024
I should probably add that this doesn't seem to happen with other decorators, e.g.
@servicedoesn't seem to cause the same issue.BillyRayPreachersSon commentedon Sep 9, 2024
I've removed
istanbul, as I realise it wasn't needed (was probably left in our project'spackage.jsonfrom 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.BillyRayPreachersSon commentedon Sep 12, 2024
Aaah - I think I've found a workaround.
It looks like the
@trackeddecorator causes problems only when a default value has been specified. Compare the output from the same file with and without the default value:So I have a workaround, but it would be nice to be able to specify default values on tracked properties without this failure.
BillyRayPreachersSon commentedon Sep 12, 2024
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.
cah-brian-gantzler commentedon Sep 12, 2024
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
One would think
thiswould be undefined in this case, but if you look at the generated code, I think this actually rewrites it as a constructor assignment.BillyRayPreachersSon commentedon Sep 13, 2024
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
= nullcode in the coverage report, I might have realised a lot sooner what was happening 🙂 .kategengler commentedon Sep 13, 2024
@BillyRayPreachersSon Does this happen with other decorators or only
@tracked?BillyRayPreachersSon commentedon Sep 13, 2024
@kategengler I've only seen this issue when using
@tracked, when there's a default value, and when that default value is overridden in theconstructor.BillyRayPreachersSon commentedon Feb 10, 2025
@kategengler - I've just seen this with another decorator (
@action). In this case, thewillRemoveaction 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.cah-brian-gantzler commentedon Feb 10, 2025
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.
BillyRayPreachersSon commentedon Feb 11, 2025
Some service injections, getters, and an
ember-concurrencytask:cah-brian-gantzler commentedon Feb 11, 2025
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.