需要mock Date构造函数,并返回确定的时间,测试结果必须是可预测的,否则是不可测试的。
main.js:
let timeContent = () => {
let content = '';
let nowDate = new Date();
let nowDay = nowDate.getDay();
let nowHours = nowDate.getHours();
let nowMinutes = nowDate.getMinutes();
let nowSeconds = nowDate.getSeconds();
if (nowDay === 0 || nowDay === 6) {
content = '距离周末还有0天';
} else {
content = `距离周末还有${5 - nowDay}天${23 - nowHours}时${59 - nowMinutes}分${59 - nowSeconds}秒`;
}
return content;
};
module.exports = timeContent;
const timeContent = require('./main');
describe('1010000022139507', () => {
it('should pass', () => {
const mo