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

docs: clarify fake timer usage with user-event #1391

Merged
merged 4 commits into from May 15, 2024

Conversation

domnantas
Copy link
Contributor

fixes #1180

The docs do not clarify how to use fake timers together with user-event. Added a section with an example how to properly set it up.

Copy link

netlify bot commented May 8, 2024

Deploy Preview for testing-library ready!

Name Link
🔨 Latest commit aded0b0
🔍 Latest deploy log https://app.netlify.com/sites/testing-library/deploys/66408ca9128a4c0008f2fb3f
😎 Deploy Preview https://deploy-preview-1391--testing-library.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@@ -51,3 +51,22 @@ afterEach(() => {
jest.useRealTimers()
})
```

## Using fake timers together with `user-event`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @domnantas !
I do think that we shouldn't mention anything about user-event on this page though.
The reason for this is that this a "core" page.

For this PR the changes on this page can be reverted, and

Or do you see this differently @MatanBobi ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I 100% agree with you @timdeschryver :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I remove

`user-event` internally uses `setTimeout` to delay subsequent actions. Using
fake timers in such tests will cause timeouts, since the `setTimeout` callbacks
do not get executed. In order to allow `user-event` to advance the fake timers,
it is necessary to set [`advanceTimers`](user-event/options.mdx#advancetimers)
option in `userEvent.setup()`:

```js
const user = userEvent.setup({advanceTimers: jest.advanceTimersByTime})
```

section entirely, or move it somewhere else?

I understand "user-event" and "core" are separate scopes, but when tests break due to fake timers, the Using fake timers page in docs is the first thing I checked. Maybe there should at least a note in this page linking to the advanceTimers option?

:::note

Using fake timers together with `user-event` may cause test timeouts. See [`advanceTimers`](user-event/options.mdx#advancetimers) option for clarification.

:::

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can be removed, or moved to the user-event docs at the advance timers option (but I think it already mentions it).

I like the note 👍, let's add it to the core page (and revert the other changes).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified and moved the explanation to user-event/options.mdx. Left a short note on guides-using-fake-timers

Comment on lines -13 to -17
your tests with fake ones. This should be used sporadically and not on a regular
basis since using it contains some overhead.

When using fake timers in your tests, all of the code inside your test uses fake
timers.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still stand by removing the This should be used sporadically and not on a regular basis since using it contains some overhead sentence. Fake timers are nothing to be scared of when used correctly. And in some cases (let's say you're developing a calendar module) they need to be used in nearly all of your tests :)

Copy link
Member

@timdeschryver timdeschryver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@timdeschryver timdeschryver merged commit b2be4bb into testing-library:main May 15, 2024
4 checks passed
@timdeschryver
Copy link
Member

@all-contributors please add @domnantas for docs

Copy link
Contributor

@timdeschryver

I've put up a pull request to add @domnantas! 🎉

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.

UserEvent: UserJest fake timers require a specific user setup
3 participants