Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't advance many setTimeouts when testing retry-fn #11369

Closed
sibelius opened this issue May 3, 2021 · 2 comments
Closed

I can't advance many setTimeouts when testing retry-fn #11369

sibelius opened this issue May 3, 2021 · 2 comments

Comments

@sibelius
Copy link

sibelius commented May 3, 2021

馃悰 Bug Report

I'm trying to test my simple function that retry an async function that throw an error

The retryFn calls the async function, if the function threw an error, it wait some time (setTimeout), and then call the function again

The test is here
https://github.com/sibelius/retry-fn/blob/master/src/__tests__/retryFn.spec.ts#L29

the second test throw this error:

Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error:

it looks like the setTimeout was not resolved

To Reproduce

Steps to reproduce the behavior:

git clone https://github.com/sibelius/retry-fn.git
jest

Expected behavior

it should advance timers to pass setTimeout/delay call

Link to repl or repo (highly encouraged)

https://github.com/sibelius/retry-fn.git

envinfo

npx: installed 1 in 1.549s

  System:
    OS: macOS 11.2.3
    CPU: (4) x64 Intel(R) Core(TM) i7-5557U CPU @ 3.10GHz
  Binaries:
    Node: 14.16.1 - ~/.nvm/versions/node/v14.16.1/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.14.12 - ~/.nvm/versions/node/v14.16.1/bin/npm
  npmPackages:
    jest: ^26.6.3 => 26.6.3 

@sibelius
Copy link
Author

sibelius commented May 3, 2021

it looks like this #10602 (comment) works

import { setImmediate } from 'timers';

const autoAdvanceTimers = <Result>(
  callback: () => Promise<Result>,
) => async () => {
  const promise = callback();
  let resolved = false;
  promise.then(() => {
    resolved = true;
  });
  while (!resolved) {
    await new Promise(setImmediate);
    if (jest.getTimerCount() === 0) {
      break;
    }
    jest.advanceTimersToNextTimer();
  }
  return await promise;
};

@sibelius sibelius closed this as completed May 3, 2021
@github-actions
Copy link

github-actions bot commented Jun 3, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant