时刻特定格式的开玩笑单元测试
Jest unit test for moment specific format
我是新手,需要你的帮助
const DATE_FORMAT = 'YYYY-MM-DD HH:mm:ss';
const DEFAULT_FORMAT = 'MM-DD-YYYY';
const OUT_TIME_HOUR =23;
const OUT_TIME_MIN = 0;
const OUT_TIME_SEC= 0;
export const getTime= (status: boolean): string => {
const date: any = new Date();
let resetDate: string;
if (status) {
date.setDate(date.getDate() + 1);
resetDate = moment(date, DEFAULT_FORMAT)
.utc()
.set({ h: OUT_TIME_HOUR , m: OUT_TIME_MIN , s: OUT_TIME_SEC})
.format(DATE_FORMAT);
} else {
resetDate= moment(date, DEFAULT_FORMAT)
.utc()
.set({ h: OUT_TIME_HOUR , m: OUT_TIME_MIN , s: OUT_TIME_SEC})
.format(DATE_FORMAT);
}
return resetDate;
};
我想以开玩笑的形式编写测试用例,比如如果状态为假,那么应该 return todayDate with 23 hr 2021-01-12 23:00:00
如果状态为真,那么应该 return nextDay 2021-01-13 23:00:00
如何模拟特定时刻的任何人都可以帮忙吗?
您需要监视 global.Date
对象,并模拟一些新日期,例如:
import { getTime } from 'your-path';
describe('get time', () => {
test('should return 23:00h - today', () => {
const mockDate = new Date(1610488800000);
const spy = jest
.spyOn(global, 'Date')
.mockImplementation(() => mockDate)
expect(getTime(false)).toEqual('2021-01-12 23:00:00');
spy.mockRestore();
});
});
我是新手,需要你的帮助
const DATE_FORMAT = 'YYYY-MM-DD HH:mm:ss';
const DEFAULT_FORMAT = 'MM-DD-YYYY';
const OUT_TIME_HOUR =23;
const OUT_TIME_MIN = 0;
const OUT_TIME_SEC= 0;
export const getTime= (status: boolean): string => {
const date: any = new Date();
let resetDate: string;
if (status) {
date.setDate(date.getDate() + 1);
resetDate = moment(date, DEFAULT_FORMAT)
.utc()
.set({ h: OUT_TIME_HOUR , m: OUT_TIME_MIN , s: OUT_TIME_SEC})
.format(DATE_FORMAT);
} else {
resetDate= moment(date, DEFAULT_FORMAT)
.utc()
.set({ h: OUT_TIME_HOUR , m: OUT_TIME_MIN , s: OUT_TIME_SEC})
.format(DATE_FORMAT);
}
return resetDate;
};
我想以开玩笑的形式编写测试用例,比如如果状态为假,那么应该 return todayDate with 23 hr 2021-01-12 23:00:00
如果状态为真,那么应该 return nextDay 2021-01-13 23:00:00
如何模拟特定时刻的任何人都可以帮忙吗?
您需要监视 global.Date
对象,并模拟一些新日期,例如:
import { getTime } from 'your-path';
describe('get time', () => {
test('should return 23:00h - today', () => {
const mockDate = new Date(1610488800000);
const spy = jest
.spyOn(global, 'Date')
.mockImplementation(() => mockDate)
expect(getTime(false)).toEqual('2021-01-12 23:00:00');
spy.mockRestore();
});
});