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

jest.useRealTimers() has no effect #3815

Closed
mjesun opened this issue Jun 14, 2017 · 5 comments · Fixed by #3858
Closed

jest.useRealTimers() has no effect #3815

mjesun opened this issue Jun 14, 2017 · 5 comments · Fixed by #3858

Comments

@mjesun
Copy link
Contributor

mjesun commented Jun 14, 2017

When timers is set to fake in Jest configuration, top level calls to useRealTimers have no effect on the test behavior. Files to reproduce:

Given the following package.json file:

{
  "dependencies": {
    "jest": "^20.0.4"
  },
  "jest": {
    "timers": "fake"
  }
}

And the following test:

jest.useRealTimers();

describe('foo', () => {
  it('bar', () => {
    jest.runAllTimers();
  });
});

You can observe how executing this test will not throw any warnings on calling runAllTimers with (supposedly) real timers enabled. This is due to the fact that, after executing useRealTimers at the top level, before entering the test execution, a useFakeTimers is executed on a beforeEach (see here, on line 67).

Temporary solution was to move useRealTimers calls inside a beforeEach, but IMO it would be nice to fix it for keeping compatibility with previous versions :)

@mjesun mjesun changed the title jasmine.useRealTimers() has no effect jest.useRealTimers() has no effect Jun 15, 2017
@rogeliog
Copy link
Contributor

Maybe related to #3814

@thymikee
Copy link
Collaborator

thymikee commented Jun 19, 2017

If the "timers": "fake" option is set, Jest runs jest.useFakeTimers() before every test, so what you need to do is run jest.useRealTimers(); in specific test function or in beforeEach hook:

describe('foo', () => {
  beforeEach(() => jest.useRealTimers());
  it('bar', () => {
    jest.runAllTimers();
  });
});

Maybe we could document it better?

@thymikee
Copy link
Collaborator

Discard my previous comment, I haven't read your final thoughts. PR is on it's way :)

@mjesun
Copy link
Contributor Author

mjesun commented Jun 19, 2017

No worries! Thanks for looking at it @thymikee 😊

@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.

3 participants