jeudi 14 novembre 2019

React Testing Library - Avoid getBy?

When testing components with React Testing Library, I find myself starting with getBy*, and occasionally needing to replace it with queryBy* (for example if I need to check for the non-existence of an element). My tests end up with a mix of getBy and queryBy, and I've recently just been using queryBy for everything.

It got me thinking... is there ever a reason to use getBy?

Assertions like this fail as expected, without the need to throw an error:

expect(queryByText('Click me')).toBeInTheDocument();
expect(queryByLabel('Name').value).toBe('George')

What's the advantage of throwing an error if an element isn't found, and is there a reason not to use queryBy for all (synchronous) queries?

Aucun commentaire:

Enregistrer un commentaire