From 652ce2abb1bf6c06757634baeaac282ac9459654 Mon Sep 17 00:00:00 2001 From: Monkey Do Date: Mon, 15 Mar 2021 20:11:04 +0100 Subject: [PATCH] Replace setTimeout function in LastFM getPage tests Jest fake timers don't play well with promises and setTimeout so we replace the setTimeout function and don't have to wait for the real timeout. See https://github.com/facebook/jest/issues/7151 --- .../static/js/src/LastFMImporter.test.tsx | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/listenbrainz/webserver/static/js/src/LastFMImporter.test.tsx b/listenbrainz/webserver/static/js/src/LastFMImporter.test.tsx index 718a76c9ed..ac4b88f457 100644 --- a/listenbrainz/webserver/static/js/src/LastFMImporter.test.tsx +++ b/listenbrainz/webserver/static/js/src/LastFMImporter.test.tsx @@ -133,19 +133,20 @@ describe("getTotalNumberOfScrobbles", () => { }); describe("getPage", () => { + const originalTimeout = window.setTimeout; beforeAll(() => { - // The timeout we use in getPage's implementation does not work with Jest fake timers - // so we disable fake timers for this section - // and give enough time for the real timeouts to run - // see https://stackoverflow.com/questions/50783013/how-to-timeout-promises-in-jest - jest.useRealTimers(); - jest.setTimeout(3000 * (LASTFM_RETRIES + 2)); + // Ugly hack: Jest fake timers don't play well with promises and setTimeout + // so we replace the setTimeout function. + // see https://github.com/facebook/jest/issues/7151 + // @ts-ignore + window.setTimeout = (fn: () => void, _timeout: number): number => { + fn(); + return _timeout; + }; }); afterAll(() => { - // Back to default - jest.setTimeout(5000); - jest.useFakeTimers(); + window.setTimeout = originalTimeout; }); beforeEach(() => {