Jest를 사용하여 유닛 테스트를하는 동안 방해가되는 오류가있었습니다.Jest가 객체를 간첩 조사하지 못했습니다.
문제 :는 내부 객체 메소드
사건에 액세스하지 않는 농담 : 우리가 직접 B 메서드를 호출 할 경우, A와 b()
방법과 lol
개체있다 농담 우리가 lol.b()
라고 부르면 실패합니다.
누군가 이미이 문제가 발생 했습니까? 더 나은 해결 방법이 있습니까?
코드 :
describe('Jest bug',() => {
it('Jest fail',() => {
const lol = (() => {
const a =() => {
console.log("console a");
b();
};
const b =() => {
console.log("console b");
};
return {
a,
b
};
})();
const spy = jest.spyOn(lol, 'b');
lol.a();
expect(spy).toHaveBeenCalled()
});
it('Jest success',() => {
const lol = (() => {
const a =() => {
console.log("console OK a");
lol.b();
};
const b =() => {
console.log("console OK b");
};
return {
a,
b
};
})();
const spy = jest.spyOn(lol, 'b');
lol.a();
expect(spy).toHaveBeenCalled()
});
});