【发布时间】:2020-01-09 06:50:55
【问题描述】:
考虑代码 -
// utils.js
export const foo = async (a, b) => {
// do something
bar(a)
}
export const bar = async (a) => {
// do something
}
// utils.spec.js
const utils = require('./utils');
const barSpy = jest.spyOn(utils, 'bar');
const result = await utils.foo('a', 'b');
expect(barSpy).toHaveBeenCalledTimes(1);
测试失败 -
Error: expect(jest.fn()).toHaveBeenCalledTimes(expected)
Expected number of calls: 1
Received number of calls: 0
我阅读了https://medium.com/@DavideRama/mock-spy-exported-functions-within-a-single-module-in-jest-cdf2b61af642 和https://github.com/facebook/jest/issues/936,但无法通过多种排列解决这个问题。
你觉得这有什么问题吗?
【问题讨论】:
-
您好,尝试在调用 bar(a) 时添加等待。 export const foo = async (a, b) => { // 做一些事情 await bar(a) }
-
调试器中的调用实际上进入了方法。
-
await 的事情不走运,似乎与分享的中篇文章更相关。
-
导入方式不起作用 - stackoverflow.com/questions/51269431/jest-mock-inner-function。这是在 node.js 中
-
这和jest有关,但是export是如何工作的。您是否尝试将 b 移动到不同的文件并导出它?
标签: javascript node.js mocking jestjs