मेरे पास जेस्ट में परीक्षण करने के लिए निम्नलिखित मॉड्यूल हैं:
// myModule.js
export function otherFn() {
console.log('do something');
}
export function testFn() {
otherFn();
// do other things
}
जैसा कि ऊपर दिखाया गया है, यह कुछ नामित कार्यों का निर्यात करता है और महत्वपूर्ण रूप से testFnउपयोग करता है otherFn।
जेस्ट में जब मैं अपने यूनिट टेस्ट के लिए लिख रहा होता हूं testFn, तो मैं otherFnफंक्शन का मजाक उड़ाना चाहता हूं क्योंकि मैं नहीं चाहता कि otherFnमेरे यूनिट टेस्ट को प्रभावित करने के लिए त्रुटियां हों testFn। मेरा मुद्दा यह है कि मुझे ऐसा करने का सबसे अच्छा तरीका यकीन नहीं है:
// myModule.test.js
jest.unmock('myModule');
import { testFn, otherFn } from 'myModule';
describe('test category', () => {
it('tests something about testFn', () => {
// I want to mock "otherFn" here but can't reassign
// a.k.a. can't do otherFn = jest.fn()
});
});
किसी भी मदद / अंतर्दृष्टि की सराहना की है।
otherFnएक अलग मॉड्यूल निकालना चाहिए और उसका मजाक उड़ाना चाहिए।