Reactjs 如何测试react组件和道具的类型

Reactjs 如何测试react组件和道具的类型,reactjs,react-testing-library,Reactjs,React Testing Library,我只是想在开箱即用的react测试工具上测试类型,我想做的事情看起来很简单,但它就是不喜欢。另外,这对其他类型是否可行 test('Expect label to be of type string', () => { const testString = 'Test value' const { getByDisplayValue } = render(<Label text={testString}/>); expect(getByDisplayValue).t

我只是想在开箱即用的react测试工具上测试类型,我想做的事情看起来很简单,但它就是不喜欢。另外,这对其他类型是否可行

test('Expect label to be of type string', () => {
  const testString = 'Test value'
  const { getByDisplayValue } = render(<Label text={testString}/>);
  expect(getByDisplayValue).toBe(string);
});
test('Expect label应为string类型',()=>{
const testString='测试值'
常量{getByDisplayValue}=render();
expect(getByDisplayValue).toBe(字符串);
});

如果您想在JavaScript中测试某物的类型,您可以使用
typeof
操作符,并可以编写这样的期望值:

const aValue = 'Some string'
expect(typeof aValue).toEqual('string')

我不确定
getByDisplayValue
来自哪里,但我也不希望它是字符串?它看起来像是一个来自?

的API,看起来不错,但这并没有测试组件的特定属性,
getByDisplayValue
我假设这是我的
组件的值。我只是向您展示了一个测试一般类型的代码示例。我认为,如果没有任何其他库,您将遇到的一个问题是,您无法轻松从组件中获取道具。我建议用酶来测试那种东西