我有一个文件index.ts,其中具有以下两个功能。
index.ts-
import { parseExpression } from 'cron-parser';
const previousMillisecondTimestamp = (cron: string, endTime: number): number => {
try {
return parseExpression(cron, {
endDate: new Date(endTime),
currentDate: new Date(endTime),
utc: true
}).prev().getTime()
} catch(e) {
return 0;
}
}
const setNextRun = (nextRunTimestampMilliseconds: number, startTimestampMilliseconds: number = new Date().getTime()) => (x: JobScheduleDto): JobScheduleDto[] => {
const scheduleIterator = parseExpression(x.cron, {
/* Line1 */
startDate: new Date(x.last_run > 900 ? x.last_run - 900 : startTimestampMilliseconds),
utc: true
});
let schedule = scheduleIterator.next();
let jobQueue: JobScheduleDto[] = [
{
...x,
next_run: x.next_run ? x.next_run : startTimestampMilliseconds
}
];
for (; schedule.getTime() < nextRunTimestampMilliseconds ; schedule = scheduleIterator.next()) {
if(x.next_run === schedule.getTime() || (!x.next_run && startTimestampMilliseconds === schedule.getTime())) {
continue;
}
jobQueue = [...jobQueue, {
...x,
last_run: previousMillisecondTimestamp(x.cron, schedule.getTime()),
next_run: schedule.getTime()
}];
}
console.log('Values', previousMillisecondTimestamp);
/* Line2 */
const updatedLastRun = previousMillisecondTimestamp(x.cron, schedule.getTime()) || schedule.getTime() || x.last_run;
jobQueue = [...jobQueue, {
...x,
last_run: updatedLastRun,
next_run: schedule.getTime()
}];
return jobQueue;
}
export const testModules = {
setNextRun,
previousMillisecondTimestamp,
}
这两个函数都导出到名为testModules的对象中(仅用于测试)。 现在,为了增加分支测试的覆盖范围,我必须覆盖 Line1 和 Line2 行(在上面的文件中进行了注释)。
为了在index.spec.ts文件中执行此操作,我想在setNextRun函数中模拟函数 previousMillisecondTimestamp 和 parseExpression (外部模块cron-parser)电话。因此,我可以相应地返回输出(未定义或null),以覆盖所有分支。
index.spec.ts-
import { testModules } from "../../src/index";
import { parseExpression } from 'cron-parser';
const currentTime = new Date().getTime();
const nextRunIntervalInMilliseconds = 10 * 60 * 1000;
describe.only('setNextRun function test coverage Test', () => {
it('setNextRun without parameters nextRunTimestampMilliseconds and startTimestampMilliseconds as null and JobScheduleDto object', () => {
const mockedPreviousMillisecondTimestamp = jest.spyOn(testModules, 'previousMillisecondTimestamp').mockImplementationOnce(() => null);
console.log('mockedPreviousMillisecondTimestamp', mockedPreviousMillisecondTimestamp);
const params = { pk: "T5", sk: workdayJobSchedule, last_run: 0, next_run: 0, status: 1, cron: "0 0 4 4 * *", schedule_description: "Desc1" }
try {
testModules.setNextRun(currentTime + nextRunIntervalInMilliseconds)(params);
} catch(error) {
expect(error.message).toBe("Cannot read property 'cron' of undefined");
}
mockedPreviousMillisecondTimestamp.mockRestore();
});
});
因此在上面的文件中,当我在下面的行中编写时,我想模拟previousMillisecondTimestamp函数的功能并返回null。
const mockedPreviousMillisecondTimestamp = jest.spyOn(testModules, 'previousMillisecondTimestamp').mockImplementationOnce(() => null);
在此之下,我正在执行一个console.log函数,以检查该函数是否已被模拟,并且在那个控制台中,我得到了它的模拟,这很好。
但是当它进入setNextRun调用时,我有一个控制台将其作为Normal函数提供。
console.log('Values', previousMillisecondTimestamp);
所以我有2个问题-