Вам необходимо убедиться, что на фиктивную функцию sendNotification
есть та же ссылка, что и на функцию sendNotification
, вызываемую внутри функции checkLiquidation
.
Например
liquidation_tracker.js
:
async function checkLiquidation(sendSms) {
const requires_notification = [1, 2, 3];
if (requires_notification.length > 0 && sendSms) {
exports.sendNotification(requires_notification);
}
}
async function sendNotification() {
return null;
}
exports.checkLiquidation = checkLiquidation;
exports.sendNotification = sendNotification;
index.test.js
:
const liquidation_tracker = require('./liquidation_tracker');
describe('61657292', () => {
it('should notify if margin ratio is > 0.8', async () => {
const sendNotificationMock = jest.fn();
liquidation_tracker.sendNotification = sendNotificationMock;
await liquidation_tracker.checkLiquidation(true);
expect(sendNotificationMock).toHaveBeenCalled();
});
});
результаты модульного тестирования с отчетом о покрытии:
PASS stackoverflow/61657292/liquidation_tracker.test.js (12.385s)
61657292
✓ should notify if margin ratio is > 0.8 (4ms)
------------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------------------|---------|----------|---------|---------|-------------------
All files | 83.33 | 75 | 50 | 83.33 |
liquidation_tracker.js | 83.33 | 75 | 50 | 83.33 | 9
------------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 14.201s