Jest:调用内部函数

问题描述 投票:1回答:1

鉴于以下内容,当执行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');
});
jestjs
1个回答
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')
})
© www.soinside.com 2019 - 2024. All rights reserved.