【发布时间】:2019-04-17 18:57:24
【问题描述】:
我有以下函数负责根据服务器返回的 Unix 时间戳返回一个布尔值,具体取决于截止期是否处于活动状态。
函数如下:
export const isCutOffTimePeriod = function (currentDateTime) {
const format = 'HH:mm a'
const currentTime = moment(currentDateTime).utcOffset(-300)
const openingTime = moment('8:00 am', format)
const closingTime = moment('2:30 pm', format)
return !currentTime.isBetween(openingTime, closingTime)
}
我正在 Jest 中为此功能编写测试:
it('Should allow a transfer between 8:30am and 2:30pm ', () => {
const currentDateTime = mockData.dateTime.nineAm // 1527862941959 - Friday,June 1, 2018 9:22:21.959 AM GMT-05:00
expect(Utils.isCutOffTimePeriod(currentDateTime)).toEqual(false)
})
即使基于输出的所有时间都正确地存储在基于日志的时刻对象中,测试也失败了,如何仅根据小时和分钟将 unix 时间戳与其他时间进行比较?
【问题讨论】:
-
[jest] 标签是指 js 测试框架,对吗?那么正确的标签是 [jestjs]。 [jest] 标签用于 ElasticSearch 的 Java HTTP Rest 客户端。
-
工作正常,因为 9.20am 介于 8.30am 和 2.30pm 之间
-
我的测试失败了,因为函数返回 true
-
您是否要检查转换为本地时间的时间戳是否在上午 8:30 到下午 2:30 之间?
标签: javascript react-native jestjs momentjs