Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

fireEvent in React Testing Library does not change the input value

Errorexpect(input).toHaveValue('new') fails: received the old value

Short 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.

  • Applies to @testing-library/react 16, @testing-library/user-event 14, Jest 29, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1The call is fireEvent.change(input) or fireEvent.input(input) with one argumentThe event has no target valueFix
    2. 2The input has type="date" and the value looks like 24/05/2020A date input receives a non-ISO stringFix
    3. 3The component reacts to key presses or focus, not only to a change eventTyping is simulated with a low-level eventFix

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.jsx

Confirmed by testing-library.com

Did this fix it?

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

Did this fix it?

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-event

2Fix

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.jsx

Confirmed by testing-library.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.