ฉันกำลังพยายามเยาะเย้ยฟังก์ชันที่ไม่ได้ส่งออกผ่าน 'jest' และ 're-wire'
ที่นี่ฉันกำลังพยายามเยาะเย้ย 'iAmBatman' (ไม่มีเจตนาเล่นสำนวน) แต่ไม่ได้ส่งออก
ดังนั้นฉันจึงใช้ rewire ซึ่งทำงานได้ดี แต่ jest.mock ไม่ทำงานอย่างที่คาดไว้
ฉันขาดอะไรบางอย่างที่นี่หรือมีวิธีง่ายๆในการบรรลุเป้าหมายเดียวกันหรือไม่?
ข้อความแสดงข้อผิดพลาดที่กำหนดโดย jest คือ:
Cannot spy the property because it is not a function; undefined given instead
service.js
function iAmBatman() {
return "Its not who I am underneath";
}
function makeACall() {
service.someServiceCall(req => {
iAmBatman();
});
return "response";
}
module.export = {
makeACall : makeACall;
}
jest.js
const services = require('./service');
const rewire = require('rewire');
const app = rewire('./service');
const generateDeepVoice = app.__get__('iAmBatman');
const mockDeepVoice = jest.spyOn(services, generateDeepVoice);
mockDeepVoice.mockImplementation(_ => {
return "But what I do that defines me";
});
describe(`....', () => {
test('....', done => {
services.makeACall(response, () => {
});
});
})