Code coverage istanbuljs with callback function, like in removeEventListener

Viewed 124

istanbuljs does not report the code coverage properly in case of callback function like in this example:

obj.removeEventListener('unload', () => this.dispose());

test case:

  it('dispose call removeEventListener', async() => {

    spyOn(obj, 'removeEventListener');

    obj.dispose();

    expect(obj['removeEventListener']).toHaveBeenCalledTimes(1);
    expect(obj['removeEventListener']).toHaveBeenCalledWith('unload', jasmine.any(Function));
  });
1 Answers

I just saved in a separated function the callback function to fix the istanbuljs code coverage report.

obj.callDispose = () => { this.dispose() }; // obj.dispose();
obj.removeEventListener('unload', this.callDispose);  // obj.callDispose

test case:

  it('dispose call removeEventListener', async() => {

    spyOn(obj, 'removeEventListener');

    obj.dispose();

    expect(obj['removeEventListener']).toHaveBeenCalledTimes(1);
    expect(obj['removeEventListener']).toHaveBeenCalledWith('unload', obj.callDispose);
  });
Related