menu

Questions & Answers

Jasmine testing: Expected Undefined to be false

new to Angular and testing.

In this method in the main code

private isReIndexActive(view: ViewShortDto): boolean {
    return view.dataFlowTaskName && this.isViewCompleted(view);
}

I created a test case in the

it('should check if Re-Run Index is not active', () => {
    const isActionEnabled = service.itemActions[4].isActive(SHORT_VIEWS[3]);

    expect(isActionEnabled).toBeFalse();
});

and the mock data is

{
    systemName: 'xxx-xxx-xxx',
    name: 'Demo',
    step: 6,
    indexStatus: 'Completed',
    dataFlowTaskName: null,
  }

when I run the unit test, the the output result is expected null to be false. however the return type in the main code is boolean. why is this error being shown?

Changed the code to tobeFalsy() however, the code review told it is not best practise.

Comments:
2023-01-19 16:42:03
2023-01-19 16:42:03
view.dataFlowTaskName && this.isViewCompleted(view); will return null because view.dataFlowTaskName is null -> null is falsy -> the operator short-circuits and returns the first falsy value. Congratulations - you've found a bug in your application. Apparently it doesn't expect that the value could be null since the method is typed as returning boolean. Most likely you don't have strictNullChecks enabled in the TS compiler options which would have notified you about this.
2023-01-19 16:42:04
@VLAZ will adding a constant before the return help fix this issue? such as const variable: boolean = view.dataFlowTaskName; return variable && this.isViewCompleted(view);
2023-01-19 16:42:04
There is not enough here to answer the question. Do not post code fragments. Post examples that fully describe and reproduce the issue. stackoverflow.com/help/how-to-ask
Answers(0) :