我需要开玩笑地为一个函数编写一个测试。我有如下功能:
async function fun(conn, input){
const connection = conn.getConnection();
....
// some output gets generated from input
// const processed_input = input???;
....
const x = util.promisify(connection.query).bind(connection);
return /* await */ x(processed_input);
}
Run Code Online (Sandbox Code Playgroud)
我希望将其值processed_input传递给 function x。
我认为类似的东西.toHaveBeenCalledWith应该有效,但我不确定它如何适用于承诺的绑定函数。
我还尝试模拟查询,执行类似于调用conn.getConnection = { query: jest.fn() }之前的fun操作,但我不确定如何继续进行expect。
expect更新:到目前为止,我当前的解决方案是在查询函数中包含 jest语句。
conn.getConnection = {
query: function(processed_input){ //expect processed_input to be; }
}`
Run Code Online (Sandbox Code Playgroud)
希望有更好的方法。