Enzyme is dead. Now what?
source link: https://dev.to/wojtekmaj/enzyme-is-dead-now-what-ekl
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
I'm the maintainer of @wojtekmaj/enzyme-adapter-react-17
. I'm here to warn you. Enzyme, a popular utility to test React components, is dead. It's time to move on. Here's why I think so.
How @wojtekmaj/enzyme-adapter-react-17
came to life
Back in August 2020, Enzyme was still the 1st choice when it comes to testing React applications, despite of how bumpy the road to Enzyme React 16 support was. Even today, 1/3 of React apps are still being tested using Enzyme!
React 17 Release Candidate came out. Shortly after, an issue has been raised in Enzyme repository to add support for React 17. Immediately after, @layershifter has opened a PR adding an official enzyme-adapter-react-17
.
Unfortunately, there were (and, spoiler alert, still are) some issues with testing that prevented this PR from being merged.
Now, here's the thing. I'm the maintainer of many popular React packages, React-PDF, React-Calendar, and React-Date-Picker just to name a few. Professionally, I maintain several large projects, which collectively have more than 30,000 Enzyme-based unit tests.
I was in no position to wait for Enzyme to add support for React 17 to support React 17 in my packages. Rewriting all unit tests also looked like no fun to me. So, I've decided to act and publish a temporary adapter based on Oleksandr's work.
It wasn't only the adapter though: certain changes were also necessary in Enzyme adapter utils, which all Enzyme adapters depend on, so a fork of them was also needed. A couple of tireless evenings later, @wojtekmaj/enzyme-adapter-react-17
was born.
Fast-forward one year
The issue for adding React 17 support remains open and has become nothing but a source of bitter comments mixed with unproductive "any updates?". Oleksandr's PR remains unmerged. enzyme-adapter-react-17
is nowhere in sight. The unofficial adapter I published with an intention of being just a stepping stone before everyone eventually migrates to enzyme-adapter-react-17
, has become de facto the default adapter for React 17, with 16 million downloads so far.
Retrospectively, I'm not sure if publishing the package was a good decision.
On one hand, this move has helped thousands of developers worldwide to upgrade to React 17, relatively hassle-free.
On the other, I helped prolonging Enzyme's inevitable death and thus gave many developers hope they shouldn't get to feel. And I'm sorry for that.
React 18
Before I came to this conclusion though, React 18 beta was announced, and of course, I jumped to see if @wojtekmaj/enzyme-adapter-react-18
could be released.
The big thing in React 18 are concurrent features. To opt-in, after upgrading to React 18, you also need to switch to the new createRoot
API:
// before
const container = document.getElementById('root');
ReactDOM.render(<App />, container);
// after
const container = document.getElementById('root');
const root = ReactDOM.createRoot(container);
root.render(<App/>);
Enter fullscreen mode
Exit fullscreen mode
Until you switch to the new API, your app will behave as if it's running React 17. So if you don't plan to do this, there's no point of upgrading!
I quickly realized that the API changes in React 18 meant that releasing a React 18 Enzyme adapter will not be possible without a huge rework of not only the adapter itself, but also Enzyme, enzyme-adapter-utils
and enzyme-adapter-react-helper
.
Trust me, it's not gonna happen. It's over. No more "stepping stones". And certainly no more official adapters. Whether or not you plan to upgrade to React 18 in the near future, you should consider looking for Enzyme alternative right now.
What should I do? 😱
The answer is, as always, it depends. Here's what I would do:
- Start familiarizing yourself with React Testing Library, an officially recommended library for React components.
- Make a rule to write new tests using RTL only.
- Consider making a rule to rewrite tests to RTL whenever you need to touch them and/or the component they are testing.
- In your designated time for repaying technical debt (you have designated time for repaying technical debt, right? …right?), rewrite your remaining Enzyme-based tests to RTL.
- Clean up your repo from Enzyme-specific bits
- When you're ready, upgrade to React 18.
A bit of personal advice
While Migrate from Enzyme support article is available, I suggest you to just start fresh, forgetting that Enzyme has ever existed. RTL is by no means an Enzyme drop-in replacement, so having a completely fresh mindset will help you getting the most of it.
Recommend
-
32
This is the second part of the series on Testing Components in React. If you have prior experience with Jest, you can skip ahead and use the GitHub code as a starting point. In theprevious article, we covered th...
-
60
Discover how to start and proceed with the testing of React components with Enzyme and Jest if you do not have enough experience in testing or if you want to freshen up your own approach.
-
36
使用 Enzyme 进行 React 组件测试进阶 2019-06-02 | 6 min read ...
-
42
Enzyme is probably the most popular tool to test React components. And though it has good competition now (see the next article!), it’s still used by many teams. This is the second article in a series, where we ...
-
15
Fig. 1: LCC outperformed all other evaluated PET hydrolases during PET-depolymerization assays.
-
35
前言 最近在给项目代码补单测,React 项目,第一反应就是成熟的解决方案 Jest + enzyme。按说读读 API,三两下应该就能写好,然而过程中却有点磕碰,遇到一些始料未及的问题。有些是关于 enzyme 的使用方法问题,有些则是对单...
-
10
Enzyme是由Airbnb所推出的React测试工具,它模拟了jQuery的API,提供了一系列简洁易用的功能,这将使得我们进行React组件测试时非常地得心应手。 一、渲染形式
-
13
当前,PyTorch、TensorFlow等机器学习框架已经成为了人们开发的重要工具。计算反向传播、贝叶斯推理、不确定性量化和概率编程等算法的梯度时,我们需要把所有的代码以微分型写入框架内。这对于将机器学习引入新领域带来了问题:在物理模拟、游戏引...
-
5
EnzymeMLNPrice$100.5824 Hour % Change-0.76%Market Cap$183.49M
-
7
Test that a component does not render when it has passed a certain prop value with the enzyme advertisements I am wanting to test if my Loadin...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK