Skip to content

Commit

Permalink
Replace setTimeout function in LastFM getPage tests
Browse files Browse the repository at this point in the history
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 jestjs/jest#7151
  • Loading branch information
MonkeyDo committed Mar 15, 2021
1 parent d157100 commit 652ce2a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions listenbrainz/webserver/static/js/src/LastFMImporter.test.tsx
Expand Up @@ -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(() => {
Expand Down

0 comments on commit 652ce2a

Please sign in to comment.