🐕

[jest] check x days later

2021/09/29に公開
expect.extend({
  toBeXDaysAfter (received, x, epochType = 'milliseconds') {
    const recv = (epochType === 'seconds') ? new Date(received * 1000).valueOf() : new Date(received).valueOf()
    const today = Date.now()
    const dur = recv - today
    const durDays = Math.round(dur / 1000 / 60 / 60 / 24)
    const msg = `expected ${received} to be ${x} days later`
    if (durDays === x) {
      return {
        message: () => msg,
        pass: true
      }
    } else {
      return {
        message: () => msg,
        pass: false
      }
    }
  }
})

expect(something).toHaveBeenCalledWith({
  date: expect.toBeXdaysLater(7),
})

Discussion

ログインするとコメントできます