0
describe('midnight event', () => {
  beforeEach(async () => {
    await initAgenda()

    agendaClient.define('midnightEvent', (job) => {
      console.log('Midnight event triggered')
    })
    agendaClient.every('0 0 * * *', 'midnightEvent', undefined, {
      timezone: 'UTC',
    })

    agendaClient.on('start', (job) => {
      console.log('Job started:', job.attrs.name)
    })
  })

  afterEach(async () => {
    jest.clearAllMocks()
    jest.clearAllTimers()
    jest.useRealTimers()
    await agendaClient.stop()
    // await agendaClient.cancel({})
  })

  it('triggers at midnight', () => {
    const logMessage = jest.fn()

    jest.useFakeTimers()
    // set date to tomorrow midnight relative to today
    jest.setSystemTime(new Date().setHours(24, 0, 0, 0))
    console.log(new Date())
    jest.advanceTimersByTime(60 * 1000)
    jest.runOnlyPendingTimers()

    // Verify that the event was triggered
    expect(logMessage).toHaveBeenCalledWith('Midnight event triggered')
  })
})

To reproduce my app's issue, i have setup the minimal setup to reproduce the issue I have to test daily midnight event.

The problem is that the job itself is scheduled correctly and registered to the db, but the handler function for defined job "mightnightEvent" doesn't seem to be firing at all. Is there issue with my test setup?

paul.kim1901
  • 341
  • 4
  • 15

0 Answers0