Eve*_*per 6 unit-testing typescript reactjs react-testing-library
我正在使用 React-testing-library 并在最后一行出现错误: expect (title.value).toBe("testtitle");})})。错误消息是属性 'value' 在类型 'HTMLElement' 上不存在。如何纠正此错误消息以有效地编写此代码?
测试文件
<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");})})
Run Code Online (Sandbox Code Playgroud)
Ema*_*oli 28
您应该将title变量强制转换HTMLInputElement为实际能够拥有该value属性。试试下面的代码:
const title = getByPlaceholderText("test") as HTMLInputElement;
Run Code Online (Sandbox Code Playgroud)
就我而言,
expect(
screen.getByLabelText(/barInput/).value,
).toEqual('bar value');
Run Code Online (Sandbox Code Playgroud)
抛出错误“类型‘HTMLElement’上不存在属性‘值’。”
我通过添加泛型解决了这个问题HTMLInputElement
expect(
screen.getByLabelText<HTMLInputElement>(/barInput/).value,
).toEqual('bar value');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6334 次 |
| 最近记录: |