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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runAllTimers is a superset of runAllTicks, but only sometimes #3000

Closed
cesarkawakami opened this issue Feb 24, 2017 · 1 comment · Fixed by #3915
Closed

runAllTimers is a superset of runAllTicks, but only sometimes #3000

cesarkawakami opened this issue Feb 24, 2017 · 1 comment · Fixed by #3915

Comments

@cesarkawakami
Copy link

Do you want to request a feature or report a bug?

A bug, or a documentation issue.

What is the current behavior?

The following code and test work:

// code
const tickThenTimeout = (state) => {
  process.nextTick(() => {
    setTimeout(() => {
      state.ok = true;
    });
  });
};

// test
it('tickThenTimeout should work', () => {
  const state = { ok: false };
  timers.tickThenTimeout(state);
  expect(state.ok).toBe(false);
  jest.runAllTimers();
  expect(state.ok).toBe(true);
});

This could make one believe that runAllTimers is a superset of runAllTicks. Current implementation here would also make one believe so, since it calls runAllTicks() at the beginning of the function.

But the following code/test don't work:

// code
const timeoutThenTick = (state) => {
  setTimeout(() => {
    process.nextTick(() => {
      state.ok = true;
    });
  }, 0);
};

// test
it('timeoutThenTick should work?', () => {
  const state = { ok: false };
  timers.timeoutThenTick(state);
  expect(state.ok).toBe(false);
  jest.runAllTimers();
  expect(state.ok).toBe(true);
});

My impression is that this behavior is inconsistent:

  • The docs mention that runAllTicks works exclusively for the micro-queue, and runAllTimers for the macro-queue. But the code above shows that runAllTimers affects both micro and macro tasks.

  • Even though runAllTimers dispatches micro-queues in some cases, it doesn't do so in all of them, as shown by test cases above.

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

The following repo:

https://github.com/cesarkawakami/jest-bug-runalltimers

is yarn test-able for the behavior explained above.

What is the expected behavior?

Both tests above pass / yarn test passes on given repository above.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

Jest: 19.0.2
Node: v7.6.0
Yarn: 0.18.2
OS: macOS Sierra 10.12.3

@github-actions
Copy link

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 May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant