Search code examples
typescriptjestjsistanbulvue-test-utilstest-coverage

Jest/Istanbul unit test branch coverage on var declaration


Why is var declaration being flagged for missing branch coverage in Istanbul coverage report?

enter image description here


Solution

  • || operator is kind of if...else.. conditional statement. You need to test each branch.

    E.g.

    index.ts:

    export class SomeClass {
      INCREMENT = 1;
      MIN_SCALE = 2;
      public zoomOut(this: any): void {
        const scaleVal = this.getFloorVar() || this.INCREMENT || this.MIN_SCALE;
        this.updateZoom(scaleVal);
      }
    
      public getFloorVar() {
        return 0;
      }
    
      public updateZoom(scaleVal) {
        console.log(scaleVal);
      }
    }
    

    index.spec.ts:

    import { SomeClass } from './';
    
    describe('SomeClass', () => {
      afterEach(() => {
        jest.restoreAllMocks();
      });
      it('should pass - 1', () => {
        const instance = new SomeClass();
        jest.spyOn(instance, 'getFloorVar');
        jest.spyOn(instance, 'updateZoom');
        instance.zoomOut();
        expect(instance.getFloorVar).toBeCalledTimes(1);
        expect(instance.updateZoom).toBeCalledWith(1);
      });
    
      it('should pass - 2', () => {
        const instance = new SomeClass();
        jest.spyOn(instance, 'getFloorVar').mockReturnValueOnce(22);
        jest.spyOn(instance, 'updateZoom');
        instance.zoomOut();
        expect(instance.getFloorVar).toBeCalledTimes(1);
        expect(instance.updateZoom).toBeCalledWith(22);
      });
    
      it('should pass - 3', () => {
        const instance = new SomeClass();
        instance.INCREMENT = 0;
        jest.spyOn(instance, 'getFloorVar');
        jest.spyOn(instance, 'updateZoom');
        instance.zoomOut();
        expect(instance.getFloorVar).toBeCalledTimes(1);
        expect(instance.updateZoom).toBeCalledWith(2);
      });
    });
    

    Unit test result with 100 coverage report:

    PASS  src/stackoverflow/59330476/index.spec.ts (11.68s)
      SomeClass
        ✓ should pass - 1 (30ms)
        ✓ should pass - 2 (3ms)
        ✓ should pass - 3 (2ms)
    
      console.log src/stackoverflow/59330476/index.ts:407
        1
    
      console.log src/stackoverflow/59330476/index.ts:407
        22
    
      console.log src/stackoverflow/59330476/index.ts:407
        2
    
    ----------|----------|----------|----------|----------|-------------------|
    File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
    ----------|----------|----------|----------|----------|-------------------|
    All files |      100 |      100 |      100 |      100 |                   |
     index.ts |      100 |      100 |      100 |      100 |                   |
    ----------|----------|----------|----------|----------|-------------------|
    Test Suites: 1 passed, 1 total
    Tests:       3 passed, 3 total
    Snapshots:   0 total
    Time:        12.984s
    

    enter image description here

    Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59330476