reactjs - React 测试库中的类型 'value' 不存在属性 'HTMLElement'

标签 reactjs typescript unit-testing 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?

测试文件

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

最佳答案

您应该将 title 变量转换为 HTMLInputElement 才能真正拥有 value 属性。尝试以下代码:

const title = getByPlaceholderText("test") as HTMLInputElement;

关于reactjs - React 测试库中的类型 'value' 不存在属性 'HTMLElement',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59986806/

相关文章:

CSS 模块覆盖 UI 样式

javascript - NestJS WebSocketGateway 未初始化

c# - 如何在生产中使用迁移,但不在测试中使用?

visual-studio-2008 - 更改默认的MsTest单元测试向导模板

arrays - 用形状 react proptype 数组

reactjs - Jest expect.any() 没有按预期工作

javascript - 如何在 React 中有条件地设置状态?

typescript - TypeScript 不能确保使用正确的类型解析 Promise 吗?

javascript - Angular 购物车 - 本地存储服务和订阅更改

Python: Nose 不接受单元测试