Ts-jest: TS-Jest should ignore private methods from the coverage report

Created on 7 Mar 2018  路  5Comments  路  Source: kulshekhar/ts-jest

  • Issue

TS-Jest should exclude all private methods from the coverage report.

  • Expected behavior

When running jest --coverage the coverage report should not complain about private methods not being tested

  • Output from your debug log
// type-class.ts
export class MyClass {
  public method1() {
    return `I'm public`;
  }

  private method2() {
    return `I'm private`;
  }
}
// myclass_spec.ts
import { MyClass } from '../type-class';

describe('MyClass', () => {
  it('test public method', () => {
    const x = new MyClass();
    const expectation = x.method1();
    expect(expectation).toEqual(`I'm public`);
  })
});
$ jest --coverage

---------------|----------|----------|----------|----------|-------------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files      |    66.67 |      100 |       50 |    66.67 |                   |
 type-class.ts |    66.67 |      100 |       50 |    66.67 |                 7 |
---------------|----------|----------|----------|----------|-------------------|

Most helpful comment

Sorry to resurrect a long-forgotten discussion, but I was looking for a solution to the same problem and stumbled upon this thread.

Regarding testing private methods. There are several reasons why I'd not recommend testing private methods:

  • private methods shouldn't be accessed outside of the class (breaks encapsulation), so there should be no way to test them in the first place
  • private methods are not exposed to the user and therefore can be invoked from the public or protected methods only. This implies that when you test public methods, you also test private methods in an indirect way (assuming that public method would fail if one of the private methods fail)
  • private methods are great when it comes to refactoring. If you plan to change any implementation detail in your class (but keep public interface unchanged), your test shouldn't be changed. Instead, they should be used to debug the code you refactored.

Not sure if this question is relevant to anyone, but there were two questions on the same matter so I decided to take a minute and try to address these concerns. Cheers 馃槉

All 5 comments

I think.. it makes perfectly sense to track coverage on private methods?

ts-jest doesn't handle coverage at all so there's not much that can be done here

this would be a good feature though. Any plan to implement this?
@GeeWee not really, _often_ private methods are left apart

I can't think of a single good reason not to get coverage from private methods unless you're not actually testing them and trying to claim coverage numbers you don't actually have. Am I missing something?

Sorry to resurrect a long-forgotten discussion, but I was looking for a solution to the same problem and stumbled upon this thread.

Regarding testing private methods. There are several reasons why I'd not recommend testing private methods:

  • private methods shouldn't be accessed outside of the class (breaks encapsulation), so there should be no way to test them in the first place
  • private methods are not exposed to the user and therefore can be invoked from the public or protected methods only. This implies that when you test public methods, you also test private methods in an indirect way (assuming that public method would fail if one of the private methods fail)
  • private methods are great when it comes to refactoring. If you plan to change any implementation detail in your class (but keep public interface unchanged), your test shouldn't be changed. Instead, they should be used to debug the code you refactored.

Not sure if this question is relevant to anyone, but there were two questions on the same matter so I decided to take a minute and try to address these concerns. Cheers 馃槉

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikeyakymenko picture mikeyakymenko  路  3Comments

stangerjm picture stangerjm  路  4Comments

stephenotalora picture stephenotalora  路  3Comments

masters3d picture masters3d  路  4Comments

GeeWee picture GeeWee  路  4Comments