Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use react testing library instead of enzyme #2298

Open
12 of 42 tasks
simonseyock opened this issue Jul 2, 2021 · 1 comment
Open
12 of 42 tasks

Use react testing library instead of enzyme #2298

simonseyock opened this issue Jul 2, 2021 · 1 comment

Comments

@simonseyock
Copy link
Member

simonseyock commented Jul 2, 2021

The react testing library enables to test components a lot like users would use them. Also it does not need full applications, it can just render single components and test them as if they were in a real page.

It is still quite atomic and can therefore replace the common tests formerly written in enzyme. It follows another philosophy though, that I find quite reasonable (see below). It enables to work with newer features of redux, for example the useSelector hook.

This text is from the react testing library page about enzyme:

The primary purpose of the React Testing Library is to give you confidence by testing your components in the way the user will use them. Users don't care what happens behind the scenes. They just see and interact with the output. So, instead of accessing the components' internal API, or evaluating the state, you'll get more confidence by writing your tests based on the component output.

React Testing Library aims to solve the problem that many developers face when writing tests with Enzyme which allows (and encourages) developers to test implementation details. Tests which do this ultimately prevent you from modifying and refactoring the component without changing the test. As a result, the tests slowed down your development speed and productivity, since every small change requires rewriting some part of your tests, even if the change you made does not affect the component's output.

Re-writing your tests in React Testing library is worthwhile, because you're trading tests that slow you down for tests that give you more confidence and increase your productivity in the long run.

https://testing-library.com/docs/react-testing-library/migrate-from-enzym

Guide

Find tests

  1. What user actions need to be done to test the functionality that was formerly tested with enzyme?
  2. Look in the styleguide, is there e2e functionality that could be tested additionally?
  3. Look at the props of the component, is there something that should be tested as well?

Write tests

  1. Use getRole / getText / etc. to retrieve elements. Try to be as semantic as possible. Look at the errors of these functions. They help a lot. If needed add role tags, this helps accessibility of the page as well (see point 5 for antd problems). Do only use query... methods if you want to test if an element is not available.
  2. Use @testing-library/jest-dom (i.e. expect(input).toHaveValue('some value'); or expect(element).toBeVisible();) for better test output.
  3. Use jest-fetch-mock to mock fetch
  4. Use waitFor or findRole / findText / etc. to work with async effects. They help with not wrapped in act(...) and performed state update on unmounted component warnings. These should all be resolved and can sometimes even occur only if multiple tests are run after each other. For special cases there exists TestUtil.setTimeout, try to avoid it.
  5. If testing with external frontend components (like antd Autocomplete) it is sometimes not possible to select elements semantically if they are not marked correctly. For this case I created the file Util/antdTestQueries to create the appropriate query / get / find selectors. This should only be the last resort.

Read more

Status

@simonseyock
Copy link
Member Author

This will get more relevant if we ever want to move to react >= 18. Enzyme does not support react >= 17 natively. For react 17 there existed an unofficial adapter, but the author already claimed that an adapter for react 18 will be to much work and it won't be written:
https://dev.to/wojtekmaj/enzyme-is-dead-now-what-ekl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants