比如util.js
,定义了两个函数:
const util = {
A() {
return this.B();
},
B() {
return 'B';
}
};
module.exports = util;
那么,怎么测试函数A
?
test("A=>B,case:1", () => {
let A = util.A;
expect(A()).toBe("B");
});
这么写测试会报错:TypeError: this.B is not a function
。难道要用mock
不成?
你这个的问题是因为
this
指向变了的问题,跟单测无关。你把你这代码直接运行,它也是不能跑的: