如何模拟对 logger.warn 的调用?

How to mock a call to logger.warn?

我正在练习测试优先开发,我想确保 class 中的方法总是在 warn 级别调用我的记录器并显示消息。我的 class 定义如下:

import { log4js } from '../config/log4js-config'

export const logger = log4js.getLogger('myClass')

class MyClass {
  sum(numbers) {
    const reducer = (accumulator, currentValue) => accumulator + currentValue
    const retval = numbers.reduce(reducer))
    if (retval < 0) {
      logger.warn('The sum is less than zero!')
    }
    return retval
  }
}

const myClass = new MyClass()
export { myClass }

我的测试是这样的:

import { myClass, logger } from './MyClass'
import { log4js } from '../config/log4js-config'

jest.mock('log4js')

describe('MyClass', () => {

  it('logs a warn-level message if sum is negative', () => {
    logger.warn = jest.fn()
    logger._log = jest.fn()
    myClass.sum([0, -1])
    expect(logger.warn).toHaveBeenCalled() // <--- fails
    expect(logger._log).toHaveBeenCalled() // <--- fails
  })
})

我也曾尝试在设置中模拟 log4js.Logger._log,但这似乎也没有用。任何建议表示赞赏!

模拟的问题是你需要提供模拟,对我来说最简单的方法是通过mock factory。但是我也会推荐一些重构:

import { getLogger } from 'log4js'

export const logger = getLogger('myClass')
logger.level = 'debug'

// export the class itself to avoid memory leaks
export class MyClass {
  // would consider even export just the sum function
  sum(numbers) {
    const reducer = (accumulator, currentValue) => accumulator + currentValue
    const retval = numbers.reduce(reducer))
    if (retval < 0) {
      logger.warn('The sum is less than zero!')
    }
    return retval
  }
}

import log4js from 'log4js';
import { MyClass } from "./class";

jest.mock('log4js', () => {
    // using the mock factory we mimic the library.

    // this mock function is outside the mockImplementation 
    // because we want to check the same mock in every test,
    // not create a new one mock every log4js.getLogger()
    const warn = jest.fn()
    return {
        getLogger: jest.fn().mockImplementation(() => ({
            level: jest.fn(),
            warn,
        })),
    }
})

beforeEach(() => {
    // reset modules to avoid leaky scenarios
    jest.resetModules()
})

// this is just some good habits, if we rename the module
describe(MyClass, () => {

    it('logs a warn-level message if sum is negative', () => {
        const myClass = new MyClass()
        myClass.sum([0, -1])

        // now we can check the mocks
        expect(log4js.getLogger).toHaveBeenCalledTimes(1) // <--- passes
        // check exactly the number of calls to be extra sure
        expect(log4js.getLogger().warn).toHaveBeenCalledTimes(1) // <--- passes
    })

})

也许仅仅监视记录器方法就可以做到这一点

import { myClass, logger } from './MyClass'

describe('MyClass', () => {

  it('logs a warn-level message if sum is negative', () => {
    const warnSpy = jest.spyOn(logger, 'warn').mockImplementation(() => {});
    const _logSpy = jest.spyOn(logger, '_log').mockImplementation(() => {});
    myClass.sum([0, -1])
    expect(warnSpy).toHaveBeenCalled()
    expect(_logSpy).toHaveBeenCalled()
  })
})