Home > Enterprise >  Jest not covering decorator function
Jest not covering decorator function

Time:11-04

I want to cover the folowing code usingjest:

    @Debounce(100)
    private checkDataToPositionInStep(): void {
        
        const proposalConsultData = this.proposalConsultResponseStore.get();

        if(proposalConsultData?.documentProposalData?.length > 1) {
            this.fullScreenLoaderService.hideLoader();
            this.router.navigate(['proposta-enviada']);
            return;
        }

        if(proposalConsultData?.warrantyData?.plateUf) {
            this.fullScreenLoaderService.hideLoader();
            this.router.navigate(['upload']);
        }

        if(proposalConsultData?.bankData?.branchCode) {
            this.fullScreenLoaderService.hideLoader();
            this.scrollService.next(STEP_ACCORDION.DADOS_GARANTIA.STEP);
            this.stepperService.next(STEP_ACCORDION.DADOS_GARANTIA.ID);
            return;
        }
        
        this.fullScreenLoaderService.hideLoader();
        this.scrollService.next(STEP_ACCORDION.DADOS_BANCARIOS.STEP);
        this.stepperService.next(STEP_ACCORDION.DADOS_BANCARIOS.ID);
        return;
    }

And de debounce decorator is like this:

export function Debounce(timeout: number): Function {
    return function (target, propertyKey: string, descriptor: PropertyDescriptor) {
        const original = descriptor.value;
        descriptor.value = function debounce(...args) {
            setTimeout(() => {
                original.apply(this, args);
            }, timeout);
        }
        return descriptor;
    }
}

When i run npm run:coverage all lines that are below decorators are not covering. Is there anyway to cover this lines ?

I just tried to call the checkDataToPositionStep method like this:

it('Should call checkDataToPositionInStep with only bankData', () => {ons
    const = proposalConsultMock = <any> {
      bankData: {
        branchCode: '01901'
      }
    };
    (facade as any).checkDataToPositionInStep(proposalConsultMock );
  });

And i thought that jest should cover the checkDataToPositionStep method.

CodePudding user response:

Just an advice... ideally you shouldn't care about coverage itself, you want to be sure things get tested. And with that in mind, if you want to be sure that Debounce decorator is tested, you can create a set of unit tests specifically for it.

import { Debounce } from './Debouncefile';

describe('Debounce', () => {
  it('should do something', () => {
    // Arrange
    const debounceFunction = Debounce(60);

    // Act
    const result = debounceFunction(params...);

    // Assert
    expect(result).toBe(something);
  });
});

Hope this helps!

CodePudding user response:

First of all: Are you missing an expect(...) call ? such as

    expect(
      (facade as any).checkDataToPositionInStep(proposalConsultMock)
    ).not.toThrowError();

Secondly: Decorators should be tested separately from your main components. So that you have both the checkDataToPositionInStep function tested and also the returned function from Debounce in another test file:

it('should do something', () => {
    // Create your instance
    const debounceFunction = Debounce(60);

    // Execute your function
    const result = debounceFunction(params...);

    // Expect a correct result
    expect(result).toBe(something);
  });

You might also want to check that your test file is correctly included in the coverage configuration.

Finally, I disagree with @facundo's advice on not caring about test coverage. It depends on the context and for code quality reasons you probably want to make sure that a percentage of your code is well tested while also avoiding software regressions.

  • Related