timers

Jest offers some utilities for the manipulation of time

via Gfycat

useful to test setTimeout and setInterval without having to actually wait

const TIMEOUT = 1000000;
function runAfterOneSec(cb) {
  setTimeout(() => {
    cb();
  }, TIMEOUT);
}

test("test timeout", () => {
  jest.useFakeTimers();

  const callback = jest.fn();
  jest.spyOn(window, "setTimeout");

  runAfterOneSec(callback);
  expect(setTimeout).toBeCalled(); // true
  expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), TIMEOUT); // true

  expect(callback).not.toBeCalled(); // true
  jest.runOnlyPendingTimers();

  expect(callback).toBeCalled(); // true
});

more advanced functions are avaiable to only run specific timers or advance time by a certain amount

Author: Jaga Santagostino

results matching ""

    No results matching ""