I am testing an angular project with jest (using jest-preset-angular
).
When collecting coverage, I get an uncovered branch and I don't understand why. I can reproduce the problem with 3 files.
some-dependency.ts
export class SomeDependency {}
some-service.ts
import { Injectable } from '@angular/core';
import { SomeDependency } from './some-dependency';
@Injectable()
export class SomeService {
constructor(private dependency: SomeDependency) {
console.log('service created');
}
}
some-service.spec
import { SomeService } from './some-service';
describe('DerivedClass', () => {
it('should create', () => {
expect(new SomeService(null)).toBeTruthy();
});
});
by running yarn jest --coverage some-service
, I get following coverage:
--------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------------|----------|----------|----------|----------|-------------------|
All files | 100 | 75 | 100 | 100 | |
some-dependency.ts | 100 | 100 | 100 | 100 | |
some-service.ts | 100 | 75 | 100 | 100 | 6 |
--------------------|----------|----------|----------|----------|-------------------|
in the HTML report, I get too little information on what is uncovered.
I noticed that removing @Injectable
decorator makes the coverage back to 100%
Does someone have an explanation? Is there a way to get my 100% coverage while keeping @Injectable
decorator?
Edit: I have added a console.log to prove the constructor is properly invoked. The yellow highlight is given by Istambul report and helps to see the uncovered branch. But there is no branch to me here since there is no condition.
jest-preset-angular
andts-jest
are you using? – Splendid[email protected]
and[email protected]
– Aylesbury