enzyme-on-rtl
v0.0.1
Published
Automated migration from Enzyme to React Testing Library
Downloads
131
Maintainers
Readme
enzyme-on-rtl
Automated migration from Enzyme to React Testing Library.
The Problem
Enzyme is dead. Airbnb stopped maintaining it in 2021, and there's no React 18/19 adapter. Thousands of projects are stuck on unmaintained testing code.
Migrating manually takes weeks for large codebases. The patterns are repetitive but tedious.
enzyme-on-rtl automates the conversion.
The Solution
A command-line tool that converts Enzyme test patterns to React Testing Library equivalents:
npx enzyme-on-rtl convert MyComponent.test.tsxWhat It Converts
| Enzyme Pattern | React Testing Library |
|----------------|----------------------|
| mount(<C />) | render(<C />) |
| shallow(<C />) | render(<C />) |
| wrapper.find('.btn') | screen.getByTestId('btn') |
| wrapper.text() | textContent |
| wrapper.html() | innerHTML |
| wrapper.simulate('click') | userEvent.click() |
| wrapper.find('.btn').exists() | expect(screen.queryByTestId('btn')).not.toBeInTheDocument() |
| import { mount } from 'enzyme' | RTL imports + cleanup |
Installation
# Global install (recommended for CLI use)
npm install -g enzyme-on-rtl
# Or use via npx
npx enzyme-on-rtl@latest convert ./srcUsage
Convert a single file
enzyme-on-rtl convert MyComponent.test.tsxConvert a directory
enzyme-on-rtl convert-dir ./src --output ./rtl-testsPreview changes (dry run)
enzyme-on-rtl diff MyComponent.test.tsxList all patterns
enzyme-on-rtl listOptions
| Flag | Description |
|------|-------------|
| -o, --output | Output file or directory (default: overwrite) |
| -h, --help | Show help |
| -v, --version | Show version |
Example
Before (Enzyme):
import { mount } from 'enzyme'
import Button from './Button'
describe('Button', () => {
it('calls onClick when clicked', () => {
const onClick = jest.fn()
const wrapper = mount(<Button onClick={onClick} />)
wrapper.find('.submit-button').simulate('click')
expect(onClick).toHaveBeenCalled()
expect(wrapper.text()).toContain('Submitted')
})
})After (React Testing Library):
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { cleanup } from '@testing-library/react'
import Button from './Button'
afterEach(() => cleanup())
describe('Button', () => {
it('calls onClick when clicked', async () => {
const onClick = jest.fn()
render(<Button onClick={onClick} />)
const button = screen.getByTestId('submit-button')
await userEvent.click(button)
expect(onClick).toHaveBeenCalled()
expect(screen.getByText('Submitted')).toBeInTheDocument()
})
})Patterns Supported
mount()→render()shallow()→render().find(selector)→screen.getByTestId(),getByRole(), orgetByText().text()→textContent.html()→innerHTML.simulate('click')→userEvent.click().simulate('change')→fireEvent.change().setProps()→ Re-render comment.state()→ Custom state inspection comment.instance()→ Behavior testing comment.find().exists()→expect(queryByTestId()).not.toBeInTheDocument()- Enzyme imports → RTL imports + cleanup
Development
# Install dependencies
pnpm install
# Run tests
pnpm test
# Build for production
pnpm build
# Run CLI
pnpm cli convert ./test-fixture.tsxWhy "enzyme-on-rtl"?
The name reflects the transition: Enzyme ON React Testing Library. Not just converting from Enzyme, but placing your tests ON a modern, supported testing foundation.
License
MIT © Peter W.
Contributing
Issues and PRs welcome! This tool is early-stage — bug reports and pattern suggestions help a lot.
