-
Notifications
You must be signed in to change notification settings - Fork 243
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
#1115
Comments
userEvent.click()
fails when used with vi.useFakeTimers()
, all available solutions are not working
This is an issue with Trick this piece of code into recognizing your environment as Jest. globalThis.jest = 'neitherUndefinedNorNull' |
@ph-fritsche Thanks a lot, I got what the problem is. But regarding your solutions, I think for a temporary workaround, this makes more sense to me wojtekmaj/react-async-button@2d26f21 Is there any ETA to fix this issue? since I personally don't like temp workaround to be there forever in my code🤣 Regarding the fix, just like letting users pass |
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);
});
}) |
I ran into same problem today. My work around: beforeEach(() => {
vi.useFakeTimers()
globalThis.jest = {
advanceTimersByTime: vi.advanceTimersByTime.bind(vi),
}
})
beforeAll(() => {
vi.useRealTimers()
})
test('given a new recurring question submitted: form data contains only question text', async () => {
...
const user = userEvent.setup({
advanceTimers: vi.advanceTimersByTime.bind(vi),
})
}) This worked but causes a Would appreciate if y'all fixed this. Testing library should work out of the box with Vitest imo. Full test fileimport { unstable_createRemixStub as createRemixStub } from '@remix-run/testing'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { beforeAll, beforeEach, describe, expect, test, vi } from 'vitest'
import { CreateQuestionFormComponent } from './create-question-form-component'
describe('CreateQuestionForm component', () => {
beforeEach(() => {
vi.useFakeTimers()
globalThis.jest = {
advanceTimersByTime: vi.advanceTimersByTime.bind(vi),
}
})
beforeAll(() => {
vi.useRealTimers()
})
test('given a new recurring question submitted: form data contains only question text', async () => {
const date = new Date('2022-10-20T01:00:00.000Z')
vi.setSystemTime(date)
let formData: FormData | undefined
const RemixStub = createRemixStub([
{
path: '/',
element: <CreateQuestionFormComponent />,
action: async ({ request }) => {
formData = await request.formData()
return null
},
},
])
const user = userEvent.setup({
advanceTimers: vi.advanceTimersByTime.bind(vi),
})
render(<RemixStub />)
const questionText = 'Did you go to bed between 8 and 9PM?'
await user.type(screen.getByLabelText('What is the recurring question?'), questionText)
await user.click(screen.getByRole('button', { name: /submit/i }))
const formEntries = Object.fromEntries(formData.entries())
expect(formEntries).toEqual({
text: questionText,
timestamp: date.toISOString(),
utcOffsetInMinutes: String(date.getTimezoneOffset()),
})
})
test('given form render: has cancel link to back to /questions', async () => {
const RemixStub = createRemixStub([
{
path: '/',
element: <CreateQuestionFormComponent />,
},
])
render(<RemixStub />)
expect(screen.getByRole('link', { name: /cancel/i })).toHaveAttribute('href', '/questions')
})
test('given click submit before input text: does not submit form', async () => {
let formData: FormData | undefined
const RemixStub = createRemixStub([
{
path: '/',
element: <CreateQuestionFormComponent />,
action: async ({ request }) => {
formData = await request.formData()
return null
},
},
])
const user = userEvent.setup({
advanceTimers: vi.advanceTimersByTime.bind(vi),
})
render(<RemixStub />)
await user.click(screen.getByRole('button', { name: /submit/i }))
expect(formData).toEqual(undefined)
})
}) |
Should this be added to documentation somewhere? |
Reproduction example
https://stackblitz.com/edit/vitejs-vite-askvcq?file=src/__tests__/App.test.tsx
Prerequisites
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
Expected behavior
The button is successfully clicked
Actual behavior
It makes the test timed out
User-event version
14.0.0
Environment
Additional context
Please also refer to vitest-dev/vitest#3184
The text was updated successfully, but these errors were encountered: