Lint for setting advanceTimers with user-event when using jest.useFakeTimers
Name for new rule
advance-timers-with-fake-timers
Description of the new rule
Enforce using the advanceTimers option for user-event when using jest's useFakeTimers in order to prevent testing timeouts
Testing Library feature
user-event's advanceTimers option when using fake timers
Testing Library framework(s)
user-event (across any Testing Library framework that can use it)
What category of rule is this?
Warns about a potential error
Optional: other category of rule
No response
Code examples
Using fake timers within a test directly:
jest.useFakeTimers();
const user = userEvent.setup(); // Warn - advanceTimers not set
beforeEach(() => {
jest.useFakeTimers()
});
test('some behavior with user action', async () => {
const user = userEvent.setup(); // Warn - advanceTimers not set
// Test behavior
})
Anything else?
No response
Do you want to submit a pull request to make the new rule?
No
Hi @backwardok. Thanks for your suggestion, sounds like an interesting rule!
Should it also warn when using userEvent.click(...) and similar methods directly without userEvent.setup()?
Should it also warn when using
userEvent.click(...)and similar methods directly withoutuserEvent.setup()?
The Testing Library docs only reference passing the advanceTimers to the setup method, so I don't think other similar methods should be reported.
The docs say
userEvent.click()(which will trigger setup internally)
I thought it calls userEvent.setup() and so is equivalent to the code this rule would warn about. I guess
- the documentation doesn't say so explicitly
- this is discouraged anyway and it may be better to have a different rule banning it.