Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'value' does not exist on type 'HTMLElement' in React Testing Library

I am using React-testing-library and getting an error on the last line which is: expect (title.value).toBe("testtitle");})}) . The error message is Property 'value' does not exist on type 'HTMLElement'. How can I rectify this error message in order to write this code efficiently?

Testfile

  <Router history={history}>
            <Route render={(props) => 
            <NewQuestion {...props} onSave={jest.fn()}/>}/>
        </Router>)
    const title= getByPlaceholderText("What's your question? Be specific");
    fireEvent.change(title, {target: {value: "testtitle"}})
    expect (title.value).toBe("testtitle");})})
like image 713
EverydayDeveloper Avatar asked Jan 30 '20 13:01

EverydayDeveloper


2 Answers

You should cast the title variable to HTMLInputElement to actually be able to have the value property. Try the following code:

const title = getByPlaceholderText("test") as HTMLInputElement;
like image 122
Emanuele Scarabattoli Avatar answered Oct 29 '22 13:10

Emanuele Scarabattoli


In my case,

  expect(
    screen.getByLabelText(/barInput/).value,
  ).toEqual('bar value');

throw Error "Property 'value' does not exist on type 'HTMLElement'."

I solved it by just adding generics HTMLInputElement

  expect(
    screen.getByLabelText<HTMLInputElement>(/barInput/).value,
  ).toEqual('bar value');
like image 44
mozu Avatar answered Oct 29 '22 14:10

mozu