question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[SIP-56] Adopt React Testing Library for testing React components

See original GitHub issue

[SIP] Proposal for adopting React Testing Library to test React components

Motivation

In Sept 2020, React Testing Library (RTL) has officially overtaken Enzyme as the most popular React testing library according to NPM downloads. While still widely popular, Enzyme has been running behind in catching up with all the latest React trends. It still doesn’t support the useEffect hook properly and has yet to officially support React 17.

Besides having a more promising future and a more active community, RTL also introduces a new way of thinking in writing tests. It encourages developers to focus on what users see, as opposed to how the components are implemented. Instead of checking component props and states with fine-grained internal APIs, you check the expected component outputs that are visible to the users. The official doc has more explanation.

We believe this is the right way to go and will help us write stabler tests more efficiently and with more confidence. With better support for simulated user events, we may even replace some Cypress tests with smaller scoped (and faster) React component tests.

Proposed Change

We propose to introduce React Testing Library to Superset’s frontend testing toolkit and gradually migrate Enzyme tests to it when possible.

New dependencies

@testing-library/react and @testing-library/jest-dom and a couple of related ESLint plugins will be added to superset-frontend and superset-ui’s dependencies.

Migration Plan and Compatibility

  1. Add RTL to the codebase, try converting a couple of existing components to RTL. Hammer out global setups, etc.
  2. Since there is no urgent need to migrate existing Enzyme tests, RTL and Enzyme will live side-by-side for as long as needed.
  3. However, all new React component tests is recommended to be written with RTL to take advantage of its benefits described above.

Rejected Alternatives

  • Keep status quo: the growing needs of better testing requires a better testing library.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:22 (20 by maintainers)

github_iconTop GitHub Comments

3reactions
eschuthocommented, Nov 26, 2020

Makes sense to bring this up in the next meetup. I’m hearing a need to perhaps discuss standardization (cleaning up old code), testing approaches, and/or establishing a process around style guide. Let’s get some feedback around what people are feeling would be needed at this point to address any concerns left over.

3reactions
nytaicommented, Nov 25, 2020

I think it’s quite normal for a codebase that’s >5 years old to have a lot of accumulated patterns, especially one with as many contributors as superset. Sometimes introducing a new pattern is conflated with refactoring out all the old patterns. I don’t think anyone is suggesting migrating all enzyme tests to use RTL (or whatever the latest flavor is at the moment). React has been making quite a few changes to their API, I don’t think we should limiting our use there. Similarly, enzyme has fallen behind the curve, I don’t think we should continue to fight with the lib to get it to work with newer portions of the react api.

I have found myself fighting with enzyme, to get what should be simple tests to pass, more often than not (see how ubiquitous this util has become in test files). Therefore, consider this a big

+1

from me as I suspect writing tests will become far more enjoyable with RTL in the toolbox.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Testing Library
The React Testing Library is a very light-weight solution for testing React components. It provides light utility functions on top of react-dom ...
Read more >
Testing Recipes - React
Testing Recipes. Common testing patterns for React components. Note: This page assumes you're using Jest as a test runner. If you use a...
Read more >
Testing React.js Hooks And Components: The Missing Piece
Leveraging storybook and testing-library makes integration tests a piece of cake - and I don't need more.
Read more >
React Testing - What Is It? How To Automate Testing? - Preflight
Component tests are usually executed using React libraries such as React Testing Library and Enzyme. When testing components, it's vital to render them...
Read more >
React Testing Library | Technology Radar - Thoughtworks
React Testing Library ... Adopt ? ... The JavaScript world moves pretty fast, and as we gain more experience using a framework our...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found