Index of every page on this site: llms.txt
All skills
github avatar

/react18-enzyme-to-rtl

@f10fdb8 official
by githubgithub/awesome-copilot42k stars
5,426
Run it yourself

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.

Use this Skill: https://skilld.dev/gh/github/awesome-copilot/react18-enzyme-to-rtl

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesenzyme-api-map.md

≈1.6k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Enzyme API Map - Complete Before/After

Setup / Configure

// Enzyme:
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });

// RTL: delete this entirely - no setup needed
// (jest.config.js setupFilesAfterFramework handles @testing-library/jest-dom matchers)

Rendering

// Enzyme - shallow (no children rendered):
import { shallow } from 'enzyme';
const wrapper = shallow(<MyComponent prop="value" />);

// RTL - render (full render, children included):
import { render } from '@testing-library/react';
render(<MyComponent prop="value" />);
// No wrapper variable needed - query via screen
// Enzyme - mount (full render with DOM):
import { mount } from 'enzyme';
const wrapper = mount(<MyComponent />);

// RTL - same render() call handles this
render(<MyComponent />);

Querying

// Enzyme - find by component type:
const button = wrapper.find('button');
const comp = wrapper.find(ChildComponent);
const items = wrapper.find('.list-item');

// RTL - query by accessible attributes:
const button = screen.getByRole('button');
const button = screen.getByRole('button', { name: /submit/i });
const heading = screen.getByRole('heading', { name: /title/i });
const input = screen.getByLabelText('Email');
const items = screen.getAllByRole('listitem');
// Enzyme - find by text:
wrapper.find('.message').text() === 'Hello'

// RTL:
screen.getByText('Hello')
screen.getByText(/hello/i)  // case-insensitive regex

User Interaction

// Enzyme:
wrapper.find('button').simulate('click');
wrapper.find('input').simulate('change', { target: { value: 'hello' } });
wrapper.find('form').simulate('submit');

// RTL - fireEvent (synchronous, low-level):
import { fireEvent } from '@testing-library/react';
fireEvent.click(screen.getByRole('button'));
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'hello' } });
fireEvent.submit(screen.getByRole('form'));

// RTL - userEvent (preferred, simulates real user behavior):
import userEvent from '@testing-library/user-event';
const user = userEvent.setup();
await user.click(screen.getByRole('button'));
await user.type(screen.getByRole('textbox'), 'hello');
await user.selectOptions(screen.getByRole('combobox'), 'option1');

Use userEvent for most interactions - it fires the full event sequence (pointerdown, mousedown, focus, click, etc.) like a real user. Use fireEvent only when testing specific event properties.


Assertions on Props and State

// Enzyme - prop assertion:
expect(wrapper.find('input').prop('disabled')).toBe(true);
expect(wrapper.prop('className')).toContain('active');

// RTL - assert on visible attributes:
expect(screen.getByRole('textbox')).toBeDisabled();
expect(screen.getByRole('button')).toHaveAttribute('type', 'submit');
expect(screen.getByRole('listitem')).toHaveClass('active');
// Enzyme - state assertion (NO RTL EQUIVALENT):
expect(wrapper.state('count')).toBe(3);
expect(wrapper.state('loading')).toBe(false);

// RTL - assert on what the state renders:
expect(screen.getByText('Count: 3')).toBeInTheDocument();
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();

Key principle: Don't test state values - test what the state produces in the UI. If the component renders <span>Count: {this.state.count}</span>, test that span.


Instance Methods

// Enzyme - direct method call (NO RTL EQUIVALENT):
wrapper.instance().handleSubmit();
wrapper.instance().loadData();

// RTL - trigger through the UI:
await userEvent.setup().click(screen.getByRole('button', { name: /submit/i }));
// Or if no UI trigger exists, reconsider: should internal methods be tested directly?
// Usually the answer is no - test the rendered outcome instead.

Existence Checks

// Enzyme:
expect(wrapper.find('.error')).toHaveLength(1);
expect(wrapper.find('.error')).toHaveLength(0);
expect(wrapper.exists('.error')).toBe(true);

// RTL:
expect(screen.getByText('Error message')).toBeInTheDocument();
expect(screen.queryByText('Error message')).not.toBeInTheDocument();
// queryBy returns null instead of throwing when not found
// getBy throws if not found - use in positive assertions
// findBy returns a promise - use for async elements

Multiple Elements

// Enzyme:
expect(wrapper.find('li')).toHaveLength(5);
wrapper.find('li').forEach((item, i) => {
  expect(item.text()).toBe(expectedItems[i]);
});

// RTL:
const items = screen.getAllByRole('listitem');
expect(items).toHaveLength(5);
items.forEach((item, i) => {
  expect(item).toHaveTextContent(expectedItems[i]);
});

Before/After: Complete Component Test

// Enzyme version:
import { shallow } from 'enzyme';

describe('LoginForm', () => {
  it('submits with credentials', () => {
    const mockSubmit = jest.fn();
    const wrapper = shallow(<LoginForm onSubmit={mockSubmit} />);

    wrapper.find('input[name="email"]').simulate('change', {
      target: { value: 'user@example.com' }
    });
    wrapper.find('input[name="password"]').simulate('change', {
      target: { value: 'password123' }
    });
    wrapper.find('button[type="submit"]').simulate('click');

    expect(wrapper.state('loading')).toBe(true);
    expect(mockSubmit).toHaveBeenCalledWith({
      email: 'user@example.com',
      password: 'password123'
    });
  });
});
// RTL version:
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

describe('LoginForm', () => {
  it('submits with credentials', async () => {
    const mockSubmit = jest.fn();
    const user = userEvent.setup();
    render(<LoginForm onSubmit={mockSubmit} />);

    await user.type(screen.getByLabelText(/email/i), 'user@example.com');
    await user.type(screen.getByLabelText(/password/i), 'password123');
    await user.click(screen.getByRole('button', { name: /submit/i }));

    // Assert on visible output - not on state
    expect(screen.getByRole('button', { name: /submit/i })).toBeDisabled(); // loading state
    expect(mockSubmit).toHaveBeenCalledWith({
      email: 'user@example.com',
      password: 'password123'
    });
  });
});

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts2mo4 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    The skill provides comprehensive guidance for migrating React tests from Enzyme to React Testing Library. It contains only markdown documentation and code templates, posing no security risk.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at f10fdb8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 days ago.

Activeupdated 7 months ago
  • React
  • Testing
  • enzyme
  • react-testing-library
  • migration
  • react18
  • unit-testing
  • jest

README badge

README badge for github/awesome-copilot/react18-enzyme-to-rtl

Rewrites Enzyme tests to React Testing Library for React 18, with API mappings and patterns that shift from asserting internal state to asserting visible behavior. Use this when migrating shallow/mount calls, wrapper.find(), simulate(), prop(), and state() checks to RTL equivalents.

Generated from the current SKILL.md.

Does this skill cover all Enzyme APIs?
Yes. It maps shallow, mount, find, simulate, prop, state, instance, and Enzyme configure/Adapter calls. Full before/after code is in the bundled references/enzyme-api-map.md and references/async-patterns.md.
Can I translate Enzyme tests 1:1 to RTL?
No. Enzyme tests implementation details (internal state, instance methods); RTL tests user-visible behavior. Tests asserting wrapper.state() or wrapper.instance() must be rewritten to assert visible outcomes instead.
Does this cover async patterns like waitFor and loading states?
Yes. The skill includes async-patterns.md with examples for waitFor, findBy, act(), Apollo MockedProvider, and error state testing.
What about wrapping components with providers (Apollo, Theme, etc.)?
The skill shows how to wrap with providers in RTL render calls and recommends using a project's customRender helper if available.

Generated from the current SKILL.md. These answers refresh after source changes.