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

test(fslib): restore timers after each test #4520

Merged
merged 3 commits into from
Jun 9, 2022

Conversation

merceyz
Copy link
Member

@merceyz merceyz commented Jun 4, 2022

What's the problem this PR addresses?

When calling jest.useFakeTimers we don't always restore the real timers again.

Fixes the timeouts in #4419.

How did you fix it?

Add an afterEach block that calls jest.useRealTimers in the test files that calls jest.useFakeTimers.

Checklist

  • I have read the Contributing Guide.
  • I have set the packages that need to be released for my changes to be effective.
  • I will check that all automated PR checks pass before the PR gets reviewed.

@merceyz merceyz marked this pull request as ready for review June 4, 2022 18:12
@arcanis
Copy link
Member

arcanis commented Jun 8, 2022

Wouldn't it be safer to keep using useFakeTime, and to put a jest.useRealTimers inside the finally? Otherwise forgetting to reset the timer sounds an easy mistake 🤔

@merceyz
Copy link
Member Author

merceyz commented Jun 8, 2022

Considering some of the tests didn't use useFakeTime I don't think that would help much here, we would have the same problem with "forgetting" to use useFakeTime.

@arcanis
Copy link
Member

arcanis commented Jun 9, 2022

But it'd be possible to prevent the use of the unsafe Jest function via an ESLint rule (no-restricted-syntax), whereas we can't enforce the reset being called.

Merging anyway since adding such a rule may be a bit complex to do in a generic way, but just something to keep in mind.

@arcanis arcanis merged commit fdb98d2 into master Jun 9, 2022
@arcanis arcanis deleted the merceyz/test/jest-28-timeout branch June 9, 2022 08:42
merceyz added a commit that referenced this pull request Oct 22, 2023
* test: let jest mock `Date.now`

* test: reset timers

* chore: dedupe

(cherry picked from commit fdb98d2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants