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 = `距离周末还有<span>${5-nowDay}天${23-nowHours}时${59-nowMinutes}分${59-nowSeconds}秒</span> `;
}
return content;
};
main.test.js
const timeContent = require('../src/main.js');
test('返回值是否包含距离周末还有', () => {
expect(timeContent()).toMatch('距离周末还有');
});
需要mock
Date
构造函数,并返回确定的时间,测试结果必须是可预测的,否则是不可测试的。main.js
:测试结果+测试覆盖率报告: