开玩笑:测试内部函数称为

时间:2019-04-22 23:17:17

标签: jestjs

鉴于以下情况,当执行foo函数时,如何确保使用正确的消息调用内部bar函数?谢谢。

const foo = (message) => console.log(message);

const bar = () => foo('this is a message');

test('test that the foo function is called with the correct message when the bar' +
     ' function is executed', () => {
  bar();
  expect(foo).toHaveBeenCalledWith('this is a message');
});

1 个答案:

答案 0 :(得分:1)

您需要像这样模拟foo函数:

let foo = message => console.log(message)

const bar = () => foo('this is a message')

test('test that the foo function is called with the correct message when the bar function is executed', () => {
    foo = jest.fn()
    bar()
    expect(foo).toHaveBeenCalledWith('this is a message')
})
相关问题