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

10,106

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;
Share:
10,106
EverydayDeveloper
Author by

EverydayDeveloper

Updated on July 21, 2022

Comments

  • EverydayDeveloper
    EverydayDeveloper almost 2 years

    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");})})