1

I am trying to mock a class implementing @azure/functions.Logger for unit tests in my Typescript app. The interface definition is

export interface Logger {

    /**

     * Writes streaming function logs at the default trace level.

     */

    (...args: any[]): void;

    /**

     * Writes to error level logging or lower.

     */

    error(...args: any[]): void;

    /**

     * Writes to warning level logging or lower.

     */

    warn(...args: any[]): void;

    /**

     * Writes to info level logging or lower.

     */

    info(...args: any[]): void;

    /**

     * Writes to verbose level logging.

     */

    verbose(...args: any[]): void;

}

I try

class SuperLogger implements Logger {

  error(...args: any[]): void {
     console.log('jdfnjskn');
  }
  warn(...args: any[]): void {
      throw new Error('Method not implemented.');
  }
  info(...args: any[]): void {
      throw new Error('Method not implemented.');
  }
  verbose(...args: any[]): void {
      throw new Error('Method not implemented.');
  }
}

But it gives

Type 'SuperLogger' provides no match for the signature '(...args: any[]): void'

How do I implement the nameless function? There are some other SO answers about extending the interface, but not seeing any on a class implementing the interface.

Seth Kitchen
  • 1,526
  • 19
  • 53

1 Answers1

1

How do I implement the nameless function?

If you're referring to the (...args: any[]): void; part of the type, that means that Logger needs to be a function. So for example:

const example: Logger = (...args: any[]) => {};
example.error = /*...*/
example.warn = /*...*/
example.info = /*...*/
example.verbose = /*...*/
Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98
  • You're a genius - I spent way too long on this. I didn't know a function could also have attributes! One note - the exact first line cast didn't work for me. I changed to: const example = (...args: any[]) => {}; – Seth Kitchen Dec 21 '22 at 16:05
  • Oh, oops, i messed up my code on that line. Your fix looks good. – Nicholas Tower Dec 21 '22 at 16:16