How to test for types on react components and props

Viewed 136

I just wanted to test types on out-of-the-box react testing tools, it looks pretty straight forward what I wanted to do, but it just doesn't like it. Plus, would this be possible for other types?

test('Expect label to be of type string', () => {
  const testString = 'Test value'
  const { getByDisplayValue } = render(<Label text={testString}/>);
  expect(getByDisplayValue).toBe(string);
});
1 Answers

If you want to test the type of something in JavaScript you can use the typeof operator and could write an expectation for that like this:

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

I'm not sure where getByDisplayValue comes from, but I also wouldn't expect it to be a String? it looks like an API from testing library?

Related