title: Jest: Avoiding all the casting in typescript - is there a way?
body: <p>I have recently tried to apply jest into my typescript workflow.</p>
  
  <p>I am using a standard </p>
  
  <pre><code>jest.mock("../logger.ts")
  </code></pre>
  
  <p>TO setup my mock but now to do some simple asserts, I am having to do the following.</p>
  
  <pre><code>expect(Logger).toBeCalledWith(category)
  expect((Logger as jest.Mock&lt;Logger&gt;).mock.instances[0].addContext).toBeCalledTimes(2)
  expect((Logger as jest.Mock&lt;Logger&gt;).mock.instances[0].addContext).toBeCalledWith("service", serviceDefinition)
  expect((Logger as jest.Mock&lt;Logger&gt;).mock.instances[0].addContext).toBeCalledWith("stack", stackDefinition)
  </code></pre>
  
  <p>Wow :-(, So Logger cast to jest.mock and then get the instance for 0 (be nice if there was a first() or one() method) and then get the methods on the instance.</p>
  
  <p>Anybody got a better way, am I doing this right ?</p>
  
  <p>I suppose I could create a instance variable above and then I don't have to repeat the casting.</p>
  
  <p>Any ideas ?</p>
  
  <p>Thanks in advance</p>
  
  <p>Here is the contents of the logger.ts</p>
  
  <pre><code>import { getLogger, Logger as Log4jLogger } from "log4js"
  
  import { LogLevel } from "./log-level"
  
  /**
   * Logger Instance Class - Represents an actual logger instance
   */
  
  export class Logger {
    private logger: Log4jLogger
  
    /**
     * Create a Logger.
     */
    constructor(category?: string) {
      // Obtain logger and set default log level
      this.logger = getLogger(category)
    }
  
    /**
     * Get the current log level
     * @returns {LogLevel}
     */
    get level(): LogLevel {
      return (this.logger.level as any).levelStr as LogLevel
    }
  
    /**
     * Set the current log level
     * @param {LogLevel} level
     */
    set level(level: LogLevel) {
      this.logger.level = level
    }
  
    /**
     * Write a trace log message
     * @param {string} message
     * @param args
     */
    trace(message: string, ...args: any[]) {
      this.logger.trace(message, ...args)
    }
  
    isTraceEnabled(): boolean {
      return this.logger.isTraceEnabled()
    }
  
    /**
     * Write a debug log message
     * @param {string} message
     * @param args
     */
    debug(message: string, ...args: any[]) {
      this.logger.debug(message, ...args)
    }
  
    isDebugEnabled(): boolean {
      return this.logger.isDebugEnabled()
    }
  
    /**
     * Write a info log message
     * @param {string} message
     * @param args
     */
    info(message: string, ...args: any[]) {
      this.logger.info(message, ...args)
    }
  
    isInfoEnabled(): boolean {
      return this.logger.isInfoEnabled()
    }
  
    /**
     * Write a warn log message
     * @param {string} message
     * @param args
     */
    warn(message: string, ...args: any[]) {
      this.logger.warn(message, ...args)
    }
  
    /**
     *
     */
    isWarnEnabled(): boolean {
      return this.logger.isWarnEnabled()
    }
  
    /**
     * Write a error log message
     * @param {string} message
     * @param args
     */
    error(message: string, ...args: any[]) {
      this.logger.error(message, ...args)
    }
  
    isErrorEnabled(): boolean {
      return this.logger.isErrorEnabled()
    }
  
    /**
     * Write a fata log message
     * @param {string} message
     * @param args
     */
    fatal(message: string, ...args: any[]) {
      this.logger.fatal(message, ...args)
    }
  
    isFatalEnabled(): boolean {
      return this.logger.isFatalEnabled()
    }
  
    addContext(key: string, value: any): void {
      this.logger.addContext(key, value)
    }
  
    removeContext(key: string): void {
      this.logger.removeContext(key)
    }
  
    clearContext(): void {
      this.clearContext()
    }
  }
  
  </code></pre>