跳到正文
Skillpedia
测试40k · #7385

react18-enzyme-to-rtl

它能做什么

这个工具可以帮你更新代码测试。如果你之前使用旧工具 Enzyme 来测试 React 代码,它能帮你切换到新工具 React Testing Library,后者更适合 React 18。

适用场景

  • ✓当你需要把旧的 Enzyme 测试更新到可以在 React 18 下运行时。
  • ✓当你升级 React 项目后,Enzyme 测试不再能正常运行时。
  • ✓当你希望通过关注用户看到和做了什么来测试 React 代码,而不是内部细节时。

技能链接

https://github.com/github/awesome-copilot/blob/main/skills/react18-enzyme-to-rtl/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/react18-enzyme-to-rtl/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我想把一个检查组件内部状态的 Enzyme 测试,改写成使用 React Testing Library,应该怎么做?”

每周 5 个新技能,1 分钟读完。

工作原理

工具会把旧的 Enzyme 函数对应到新的 React Testing Library 函数上。它会告诉你如何重写测试,让测试聚焦于用户可见的行为,而不是代码的内部细节。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

React 18 Enzyme → RTL Migration

Enzyme has no React 18 adapter and no React 18 support path. All Enzyme tests must be rewritten using React Testing Library.

The Philosophy Shift (Read This First)

Enzyme tests implementation. RTL tests behavior.

// Enzyme: tests that the component has the right internal state
expect(wrapper.state('count')).toBe(3);
expect(wrapper.instance().handleClick).toBeDefined();
expect(wrapper.find('Button').prop('disabled')).toBe(true);

// RTL: tests what the user actually sees and can do
expect(screen.getByText('Count: 3')).toBeInTheDocument();
expect(screen.getByRole('button', { name: /submit/i })).toBeDisabled();

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.

API Map

For complete before/after code for each Enzyme API, read:

  • references/enzyme-api-map.md - full mapping: shallow, mount, find, simulate, prop, state, instance, configure
  • references/async-patterns.md - waitFor, findBy, act(), Apollo MockedProvider, loading states, error states

Core Rewrite Template

// Every Enzyme test rewrites to this shape:
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import MyComponent from './MyComponent';

describe('MyComponent', () => {
  it('does the thing', async () => {
    // 1. Render (replaces shallow/mount)
    render(<MyComponent prop="value" />);

    // 2. Query (replaces wrapper.find())
    const button = screen.getByRole('button', { name: /submit/i });

    // 3. Interact (replaces simulate())
    await userEvent.setup().click(button);

    // 4. Assert on visible output (replaces wrapper.state() / wrapper.prop())
    expect(screen.getByText('Submitted!')).toBeInTheDocument();
  });
});

RTL Query Priority (use in this order)

  1. getByRole - matches accessible roles (button, textbox, heading, checkbox, etc.)
  2. getByLabelText - form fields linked to labels
  3. getByPlaceholderText - input placeholders
  4. getByText - visible text content
  5. getByDisplayValue - current value of input/select/textarea
  6. getByAltText - image alt text
  7. getByTitle - title attribute
  8. getByTestId - data-testid attribute (last resort)

Prefer getByRole over getByTestId. It tests accessibility too.

Wrapping with Providers

// Enzyme with context:
const wrapper = mount(
  <ApolloProvider client={client}>
    <ThemeProvider theme={theme}>
      <MyComponent />
    </ThemeProvider>
  </ApolloProvider>
);

// RTL equivalent (use your project's customRender or wrap inline):
import { render } from '@testing-library/react';
render(
  <MockedProvider mocks={mocks} addTypename={false}>
    <ThemeProvider theme={theme}>
      <MyComponent />
    </ThemeProvider>
  </MockedProvider>
);
// Or use the project's customRender helper if it wraps providers
常见问题

为什么我不能继续使用 Enzyme 了?

Enzyme 已经不再更新,也无法很好地支持较新的 React 版本(比如 React 18)。现在推荐使用 React Testing Library。

“测试行为”而不是“测试实现”是什么意思?

测试实现(Enzyme)关注代码的内部细节。测试行为(React Testing Library)关注最终用户在屏幕上看到什么,以及他们如何与应用互动。

旧的 Enzyme 测试能直接转换吗?

不完全能。由于思路不同(测试行为,而非测试实现),一些检查内部细节的旧测试没有直接对应的写法,你需要重写它们,改为检查可见的结果。

详情

作者的原始描述

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 imp

repo: github/awesome-copilot

热度

GitHub 星标
40k
排名
#7385
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能