笑话:避免打字稿中的所有类型转换-有办法吗?

时间:2019-06-02 15:23:25

标签: typescript jestjs

我最近尝试将笑话应用到我的打字稿工作流程中。

我使用的是标准

jest.mock("../logger.ts")

要设置我的模拟,但现在要做一些简单的断言,我必须执行以下操作。

expect(Logger).toBeCalledWith(category)
expect((Logger as jest.Mock<Logger>).mock.instances[0].addContext).toBeCalledTimes(2)
expect((Logger as jest.Mock<Logger>).mock.instances[0].addContext).toBeCalledWith("service", serviceDefinition)
expect((Logger as jest.Mock<Logger>).mock.instances[0].addContext).toBeCalledWith("stack", stackDefinition)

哇:-(,因此Logger强制转换为jest.mock,然后将实例获取为0(如果有first()或one()方法,那就很好了),然后在实例上获取方法。

任何人都有更好的方法,我这样做对吗?

我想我可以在上面创建一个实例变量,然后不必重复转换。

有什么想法吗?

预先感谢

这是logger.ts的内容

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()
  }
}

0 个答案:

没有答案