React 18 Enzyme to RTL

React 18 Enzyme to RTL

Explains how to rewrite Enzyme tests into React Testing Library for React 18, shifting from implementation checks to behaviour tests with API mappings and async patterns.

Skill files3 files

references/

SKILL.md94 lines
1
---
2
name: react18-enzyme-to-rtl
3
description: 'Provides exact Enzyme → React Testing Library migration patterns for React 18 upgrades. Use this skill whenever Enzyme tests need to be rewritten - shallow, mount, wrapper.find(), wrapper.simulate(), wrapper.prop(), wrapper.state(), wrapper.instance(), Enzyme configure/Adapter calls, or any test file that imports from enzyme. This skill covers the full API mapping and the philosophy shift from implementation testing to behavior testing. Always read this skill before rewriting Enzyme tests - do not translate Enzyme APIs 1:1, that produces brittle RTL tests.'
4
---
5
6
# React 18 Enzyme → RTL Migration
7
8
Enzyme has no React 18 adapter and no React 18 support path. All Enzyme tests must be rewritten using React Testing Library.
9
10
## The Philosophy Shift (Read This First)
11
12
Enzyme tests implementation. RTL tests behavior.
13
14
```jsx
15
// Enzyme: tests that the component has the right internal state
16
expect(wrapper.state('count')).toBe(3);
17
expect(wrapper.instance().handleClick).toBeDefined();
18
expect(wrapper.find('Button').prop('disabled')).toBe(true);
19
20
// RTL: tests what the user actually sees and can do
21
expect(screen.getByText('Count: 3')).toBeInTheDocument();
22
expect(screen.getByRole('button', { name: /submit/i })).toBeDisabled();
23
```
24
25
This is not a 1:1 translation. Enzyme tests that verify internal state or instance methods don't have RTL equivalents - because RTL intentionally doesn't expose internals. **Rewrite the test to assert the visible outcome instead.**
26
27
## API Map
28
29
For complete before/after code for each Enzyme API, read:
30
- **`references/enzyme-api-map.md`** - full mapping: shallow, mount, find, simulate, prop, state, instance, configure
31
- **`references/async-patterns.md`** - waitFor, findBy, act(), Apollo MockedProvider, loading states, error states
32
33
## Core Rewrite Template
34
35
```jsx
36
// Every Enzyme test rewrites to this shape:
37
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
38
import userEvent from '@testing-library/user-event';
39
import MyComponent from './MyComponent';
40
41
describe('MyComponent', () => {
42
it('does the thing', async () => {
43
// 1. Render (replaces shallow/mount)
44
render(<MyComponent prop="value" />);
45
46
// 2. Query (replaces wrapper.find())
47
const button = screen.getByRole('button', { name: /submit/i });
48
49
// 3. Interact (replaces simulate())
50
await userEvent.setup().click(button);
51
52
// 4. Assert on visible output (replaces wrapper.state() / wrapper.prop())
53
expect(screen.getByText('Submitted!')).toBeInTheDocument();
54
});
55
});
56
```
57
58
## RTL Query Priority (use in this order)
59
60
1. `getByRole` - matches accessible roles (button, textbox, heading, checkbox, etc.)
61
2. `getByLabelText` - form fields linked to labels
62
3. `getByPlaceholderText` - input placeholders
63
4. `getByText` - visible text content
64
5. `getByDisplayValue` - current value of input/select/textarea
65
6. `getByAltText` - image alt text
66
7. `getByTitle` - title attribute
67
8. `getByTestId` - `data-testid` attribute (last resort)
68
69
Prefer `getByRole` over `getByTestId`. It tests accessibility too.
70
71
## Wrapping with Providers
72
73
```jsx
74
// Enzyme with context:
75
const wrapper = mount(
76
<ApolloProvider client={client}>
77
<ThemeProvider theme={theme}>
78
<MyComponent />
79
</ThemeProvider>
80
</ApolloProvider>
81
);
82
83
// RTL equivalent (use your project's customRender or wrap inline):
84
import { render } from '@testing-library/react';
85
render(
86
<MockedProvider mocks={mocks} addTypename={false}>
87
<ThemeProvider theme={theme}>
88
<MyComponent />
89
</ThemeProvider>
90
</MockedProvider>
91
);
92
// Or use the project's customRender helper if it wraps providers
93
```
94

© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 11,971 prompts

More by @OpenSourceBuffGuy

See all
Paris Shopfront Editorial Portrait prompt image

Paris Shopfront Editorial Portrait

Generates a moody film-scanned editorial portrait of a young woman in an oversized leather coat outside a vintage Paris wine shop at dusk.

image-prompt, nano-banana, negative-prompt, portrait, woman, medium-shot, editorial, fashion, film-grain, muted-colors, cinematic, photorealistic, vertical-format, text-rendering, soft-lighting, moody, bokeh, cityscape

Pastel autumn outfit lookbook with handwritten annotations prompt image

Pastel autumn outfit lookbook with handwritten annotations

Creates a pastel e-commerce fashion lookbook poster of a model in a simple autumn outfit, with handwritten labels, arrows and a bold headline.

image-prompt, fashion, fashion-shoot, poster, text-rendering, pastel, autumn, full-body, woman, minimalist, negative-space, vertical-format, gpt-image, product-photography, social-media-graphic, photorealistic, for-online-sellers, for-designers, portrait, editorial, whimsical, cozy, studio-lighting

Premium food photography template prompt image

Premium food photography template

Creates a steaming, glossy spicy Sichuan-style dish photo in a dark bowl with chilies and restaurant props, with the food and ratio left to fill in.

image-prompt, gpt-image, food-photography, photorealistic, cinematic, moody, dramatic-lighting, bokeh, close-up, high-angle, square-format, placeholders, template, negative-prompt, cooking, restaurants, product-photography, for-photographers, for-small-business-owners, for-online-sellers, advertising

Reviews

Loading reviews…

Discussion

Loading comments…