How to unit test a void method

10,141

You can test its side effects using spies, for example:

describe('error method', => {
    it('should log an error on the console', () => {
        spyOn(console, 'error');

        error(...);

        expect(console.error).toHaveBeenCalledWith(...);
    });

    ...
});
Share:
10,141

Related videos on Youtube

Bahodir
Author by

Bahodir

Updated on June 04, 2022

Comments

  • Bahodir
    Bahodir almost 2 years

    I am trying to reach more code coverage. I have an "informational" method that just triggers notification, and response is not required. How do I unit test it?

    public error(message?: any, ...optionalParams: any[]) {
        if (this.isErrorEnabled()) {
          console.error(`${this.name}: ${message}`, ...optionalParams);
        }
      }