[英]How to test a function that returns an arrow function with Jest?
I have a function like this:我有一个这样的 function:
export const myFunc = (a, b ,callBack) => {
const timer = setTimeout(() => {
callBack(a+b);
}, 10);
return () => clearTimeout(timer);
};
I'm able to test the callback function just fine by doing this:通过这样做,我可以很好地测试回调 function:
jest.useFakeTimers();
describe('myFunc', () => {
const callBack = jest.fn();
it('runs myFunc', () => {
myFunc(1, 2, callBack);
jest.runAllTimers();
expect(callBack).toHaveBeenCalledWith(3);
});
However, the last line return () => clearTimeout(timer)
is never tested somehow.但是,最后一行
return () => clearTimeout(timer)
从未以某种方式进行过测试。 Jest report just says this line function not covered
and statement not covered
.开玩笑的报告只是说这一行
function not covered
并且statement not covered
。 Can anyone please tell me a way to cover this line without using any other external testing library like enzyme?谁能告诉我一种方法来覆盖这一行而不使用任何其他外部测试库(如酶)?
The function isn't called, so it's not covered. function 没有被调用,所以它没有被覆盖。
Another test would be:另一个测试是:
let cleanup = myFunc(1, 2, callBack);
cleanup()
jest.runAllTimers();
expect(callBack).not.toHaveBeenCalled();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.