sandpack icon indicating copy to clipboard operation
sandpack copied to clipboard

Fake timers support for tests

Open jsartisan opened this issue 3 months ago • 0 comments

Is there a way to use fakeTimers in sandpack's tests component?

Currently I am forced to use realTimers. It works but it takes a long time to see the tests results ( because of real timers ofcourse ). The way I am writing tests is using a "wait" utility

import { debounce } from "./index";

function wait(time) {
  return new Promise((resolve) => setTimeout(resolve, time));
}

describe("Debounce Function", () => {
  it("should not be invoked immediately", async () => {
    const originalFunction1 = jest.fn();
    const debouncedFunction1 = debounce(originalFunction1, 300);
    debouncedFunction1();
    expect(originalFunction1).not.toBeCalled();
  });
});

Is there a better way?

jsartisan avatar Sep 28 '25 07:09 jsartisan