Test gets stuck when using jest.useFakeTimers() during a database async operation

227 views Asked by At

I'm trying to do some tests where I need to use fake timers. I'm not showing the complete test here because the issue can be reduced to this. The point is that when I debug, it gets stuck on the await triggerRepository. If I remove the fake timers the await resolves and the test goes well. What am I doing wrong? Thank you!

describe('Get all triggers', () => {

    beforeEach(async () => {
      jest.useFakeTimers();
    });

    afterEach(() => {
      jest.runOnlyPendingTimers();
      jest.useRealTimers();
    });

    test('trigger status updates true', async () => {
      const savedTrigger = await triggerRepository.findOne(trigger.id);
      expect(savedTrigger.status).toBe(TriggerStatus.TRIGGER_STATUS_REGULAR);
    });
  });

I tried advancing the timers after the async call, but the test doesn't reach that line.

I'm using "ts-jest": "^27.0.3" and "jest": "^27.2.5"

0

There are 0 answers