-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
userEvent.click()
fails when used with vi.useFakeTimers()
, all available solutions are not working
#3184
Comments
Start a new pull request in StackBlitz Codeflow. |
Seems it's not related to testing-library/react-testing-library#1197 One workaround could be like this: In your test suites using fake timers import { beforeAll, vi, describe } from 'vitest';
describe('this suite uses fake timers', () => {
// Temporarily workaround for bug in @testing-library/react when use user-event with `vi.useFakeTimers()`
beforeAll(() => {
const _jest = globalThis.jest;
globalThis.jest = {
...globalThis.jest,
advanceTimersByTime: vi.advanceTimersByTime.bind(vi)
};
return () => void (globalThis.jest = _jest);
});
}) |
Hey @sheremet-va, a simple question here that is not clear in the documentation: Regarding E.g. consider the following code
import { beforeAll, vi, describe, it, expect } from 'vitest'
// This applies to the whole file, which is the outer scope
beforeAll(() => {
vi.stubGlobal('innerWidth', 100)
return () => void vi.unstubAllGlobals()
})
describe('A test suite, which is a inner scope', () => {
// This applies only to this test suite
beforeAll(() => {
vi.stubGlobal('innerHeight', 200)
return () => void vi.unstubAllGlobals() // Will this restore `innerWidth` as well?
})
it('is a test', () => {
expect(window.innerHeight).toBe(200) // This is guaranteed to be true
expect(window.innerWidth).toBe(100) // This is guaranteed to be true as well
)}
)}
// Is my assumption correct?
describe('Another test suite, which runs after the first one OR NOT?', () => {
it('is a test', () => {
// This is guaranteed to be FALSE this it's cleared after the previous test suite has been done
expect(window.innerHeight).toBe(200)
// But what about this one? Is `innerWidth` cleared in teardown for the first suite?
expect(window.innerWidth).toBe(100)
)}
}) |
Describe the bug
I have a React component which is a timer (minimal reproduction), that starts automatically after mounted, and there's a button to RESET the timer.
In the test, I'm using
vi.useFakeTimers()
andawait vi.advanceTimersByTimeAsync(500)
to test the timer segmentally.However, I'm not able to use
await user.click()
to click the button.This is a known issue with fake timers, however, none of the existing solutions works.
None of these works
And I cannot use
vi.useRealTimers()
before clicking button, since it will break the fake timer to further test the component's reset functionality.Those solutions above are all based on
Jest
since almost all resources on the internet are forJest
. But since I've never usedJest
, so I'm not sure if it's working inJest
, and is this an issue with@testing-library/user-event
orVitest
Reproduction
https://stackblitz.com/edit/vitejs-vite-askvcq?file=src/__tests__/App.test.tsx
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: