fireEvent in React Testing Library does not change the input value
expect(input).toHaveValue('new') fails: received the old valueShort answer
fireEvent.change needs a second argument that carries the new value as target: { value }. Without it nothing is assigned to the element. Date inputs also need an ISO date. For typing, the library's own advice is user-event, whose calls return promises and must be awaited.
Fix it fast
1Find out which cause you have
Print the value right after the event. If it is still the old one, compare your call with the shapes below.
fireEvent.change(input, { target: { value: 'abc' } }); console.log(input.value);2Match what you see, then jump to its fix
- 1
The call is fireEvent.change(input) or fireEvent.input(input) with one argumentThe event has no target valueFix - 2
The input has type="date" and the value looks like 24/05/2020A date input receives a non-ISO stringFix - 3
The component reacts to key presses or focus, not only to a change eventTyping is simulated with a low-level eventFix
- 1
What does the error look like?
expect(input).toHaveValue('new') fails: received the old value
Cause 1 of 3
The event has no target value
1Check
The Testing Library events page shows the shape fireEvent.change(element, {target: {value: 'a'}}). The second argument assigns properties to the element before the event fires.
grep -rn "fireEvent\.\(change\|input\)" src/2Fix
Pass the value inside target.
import { render, screen, fireEvent } from '@testing-library/react';
render(<Search />);
const input = screen.getByLabelText(/search/i);
fireEvent.change(input, { target: { value: 'boots' } });
expect(input).toHaveValue('boots');3Verify
The assertion passes.
npx jest src/Search.test.jsxConfirmed by testing-library.com
Cause 2 of 3
A date input receives a non-ISO string
1Check
The events page says the value for a date input must be ISO 8601: a value like 24/05/2020 will not update the element, while 2020-05-24 will.
grep -rn 'type="date"' src/2Fix
Send the date in year-month-day form.
fireEvent.change(dateInput, { target: { value: '2020-05-24' } });3Verify
The input shows the date.
expect(dateInput).toHaveValue('2020-05-24');Confirmed by testing-library.com
Cause 3 of 3
Typing is simulated with a low-level event
1Check
The events page says that most of the time you should use @testing-library/user-event rather than fireEvent. The user-event docs show const user = userEvent.setup() and await on every call.
npm ls @testing-library/user-event2Fix
Use user-event, and await each call; a missing await leaves the assertion running too early.
import userEvent from '@testing-library/user-event';
const user = userEvent.setup();
await user.clear(input);
await user.type(input, 'boots');
expect(input).toHaveValue('boots');3Verify
The test passes without extra waiting.
npx jest src/Search.test.jsxConfirmed by testing-library.com
How do I stop it coming back?
- Default to user-event for anything a person would type, click or tab through.
- Assert with toHaveValue so a failure shows the actual value.
- Keep fireEvent for events that user-event does not model.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
A React input keeps its old text when defaultValue changes
A component is changing an uncontrolled input to be controlled.
defaultValue only sets the starting text.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix