我想以100%的覆盖率开玩笑地测试此功能。它是一个Javascript函数。它嵌套得很深,因此我无法了解如何覆盖每条语句进行测试
/**
* Track the progress of a task from an intent response
* @param {String} taskUuid - the response of the entity to track
* @param {Number} [interval] - the polling interval to use (optional).
* @param {Object} trigger - triggers to excecute fucnctions based on task status
* @returns {Promise} promise that resolves when the task is completed.
*/
export function trackTaskProgress(taskUuid, interval, trigger) {
return new Promise((resolve, reject) => {
if (!TaskManager) {
resolve();
return;
}
TaskManager.trackTaskStatus(taskUuid, {
intervalTime: interval || 5000,
onTaskUpdate: (task) => {
const completed = task.isTaskCompleted();
if (!completed) {
if (trigger && trigger.whileNotCompleted) {
trigger.whileNotCompleted(task);
}
} else {
const isFailed = (task.isStatusAborted() || task.isStatusFailed());
if (isFailed) {
if (trigger && trigger.whenFailed) {
trigger.whenFailed(task);
}
} else {
// eslint-disable-next-line no-lonely-if
if (trigger && trigger.whenSuccess) {
trigger.whenSuccess(task);
}
}
resolve();
return completed;
}
}
});
});
}
我已经尝试过编写如下的测试用例:
describe('ANCUtils', () => {
describe('trackTaskProgress', () => {
it('test triggerOptions to have been called', () => {
AphroditeApi.Managers.TaskManager = {
trackTaskStatus: jest.fn()
};
AphroditeApi.Managers.TaskManager.trackTaskStatus.mockImplementation((trigger) => {
trigger.whileNotCompleted();
trigger.whenFailed();
trigger.whenSuccess();
});
AphroditeApi.Managers.TaskManager.trackTaskStatus(mockTriggerOptions);
trackTaskProgress('a80166d2-8689-4850-b95a-209fd5e85712',
1000, mockTriggerOptions);
expect(mockTriggerOptions.whileNotCompleted).toHaveBeenCalled();
expect(mockTriggerOptions.whenFailed).toHaveBeenCalled();
expect(mockTriggerOptions.whenSuccess).toHaveBeenCalled();
});
});
但它甚至不能覆盖10%。附加覆盖率报告中的屏幕截图。 enter image description here