Saya mencoba meniru fungsi yang tidak diekspor melalui 'jest' dan 're-wire'.
Di sini saya mencoba mengejek 'iAmBatman' (tidak ada maksud kata-kata) tetapi tidak diekspor.
Jadi saya menggunakan rewire, yang berfungsi dengan baik. Tapi jest.mock tidak berfungsi seperti yang diharapkan.
Apakah saya melewatkan sesuatu di sini atau Apakah ada cara mudah untuk mencapai hal yang sama?
Pesan kesalahan yang diberikan oleh jest adalah:
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, () => {
});
});
})