All jobs

Migrate Enzyme Tests to React Testing Library

Yes. An agent rewrites Enzyme tests as React Testing Library tests that check what the user sees, removes the adapter, and opens a pull request with the suite green.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the test files before it picks versions, because the React version decides which Testing Library release can be installed at all. In a project like the-road-to-learn-react/hackernews-client, src/App.test.js holds nine tests in three styles: four mount a component with ReactDOM.render into a detached div, four take react-test-renderer snapshots, and one uses Enzyme, shallow(<Table {...props} />) followed by element.find('.table-row').length. The file configures enzyme-adapter-react-16, and the lockfile holds React 16.8.4. Installing the current @testing-library/react fails with ERESOLVE, since it requires React 18 or 19. Version 12.1.5 is the release whose peer range is react <18.0.0, so the agent pins that major and says why.

It then checks whether the test environment can run asynchronous queries. Under react-scripts 3.1.0 the tests run on Jest 24 with jsdom 11.12.0. Synchronous queries work there, but waitFor and every findBy query fail with TypeError: MutationObserver is not a constructor. The same tests pass under react-scripts 5.0.1, which brings Jest 27 and jsdom 16. The agent reports this as a prerequisite and does not work around it with a polyfill: either the pull request includes the react-scripts upgrade, or the new tests are limited to synchronous queries until it lands. It also removes react-addons-test-utils 15.6.2 from devDependencies. No file imports it, and its peer requirement on react-dom ^15.4.2 stops npm install with ERESOLVE once the lockfile is rewritten.

Rewriting is where reading the component matters. The Enzyme test passes sortKey and isSortReverse as props, but Table in src/App.js keeps both in its own state and reads only list and onDismiss, so those props never did anything. The rows in the test data have no url, so each title renders as an anchor without href, which has no link role and is invisible to getAllByRole('link'). The agent fixes the data instead of falling back to a class selector, then tests behaviour the old file never reached: clicking a column header sorts the rows and a second click reverses them, and clicking Dismiss calls onDismiss with that row's objectID.

The App tests change the most. App calls axios in componentDidMount against hn.algolia.com, and the existing test mounts it with nothing mocked, so the suite sends a real request each time it runs. The agent mocks axios with jest.mock, resolves it with two stories, and asserts on the result with findByRole: the rows appear, a dismissed story leaves the list, a rejected request shows Something went wrong., and a pending one shows Loading .... It replaces the react-test-renderer snapshots with a small number of asFragment() snapshots where a snapshot still earns its place, then removes enzyme, the adapter and the Enzyme.configure call.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Migrate this repository's Enzyme tests to React Testing Library. Read package.json, the lockfile and every test file first, and report: the locked React version, the Jest and jsdom versions the test script runs on, each Enzyme API in use (shallow, mount, find, simulate, setState, instance and so on) with its count, and any test that makes a real network request. Pick the @testing-library/react major that supports the locked React and say why; add @testing-library/jest-dom. Before rewriting, write one throwaway test that calls waitFor and run it; if it fails because the environment has no MutationObserver, stop adding async queries and tell me what has to be upgraded, and do not add a polyfill. If npm reports ERESOLVE, find the package responsible and whether anything imports it. Rewrite each Enzyme test to query by role or text the way a user would, and fix test data that makes that impossible instead of selecting by class name. Read each component a test renders and drop props the component never reads. Mock the HTTP client so no test touches the network, and cover the loading, success, error and dismiss paths of the top-level component. Keep a snapshot only where it protects markup that matters; convert those to asFragment. Remove enzyme, its adapter and react-test-renderer once nothing imports them. Run the suite before and after and open a pull request with both summaries, the list of tests removed, rewritten and added, and anything you could not migrate.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request that rewrites the test file, regenerates its snapshot file, removes enzyme, enzyme-adapter-react-16, react-test-renderer and react-addons-test-utils from package.json, and adds @testing-library/react and @testing-library/jest-dom. The description starts with the inventory: which Enzyme calls existed, which versions the tests run on, and the result of the waitFor probe. A table maps every old test to what happened to it, so a reviewer can see that the four mount-only tests were folded into tests that assert something, and nothing was dropped quietly.

The snapshot file is the diff to read slowly. A react-test-renderer snapshot prints className and onClick={[Function]}; a DOM snapshot from asFragment() prints class and no handlers. Every stored snapshot therefore changes even when the markup did not, and the description says which snapshots were deleted outright and why.

Before merging, the reviewer checks that no test selects by CSS class where a role or text exists, that the mocked axios call is asserted with the full URL the component builds, and that the React Testing Library major matches the React version in the lockfile. If the run stopped at the MutationObserver probe, the pull request contains only synchronous tests and names the upgrade that unblocks the rest.

Run it on every repository still on Enzyme

A migration like this is started once for each repository, by the developer who wants it done. Send the body to POST /o/{org}/sessions from a terminal or an MCP client with a request_id that is a UUID made for the job, and go back to other work. When the run ends on its prerequisite question, whether to upgrade react-scripts in the same pull request, answer with a follow-up to POST /o/{org}/sessions/{id}/messages; the sandbox wakes with the dependencies installed and the inventory already done.

For a large test suite, send one run per directory of tests, each naming its directory in input and each with its own request_id. Runs beyond the key's concurrency limit wait as queued and start as slots free, so the script that sends them needs no pacing. Tell every run to leave package.json alone except the first, so the pull requests do not conflict on the dependency change.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

Whether a Testing Library test can run depends on three versions that interact: React, the library's own major, and the jsdom inside the test runner. Reading documentation gets one of them wrong more often than not. In its own sandbox the agent installs the locked dependencies, runs a probe, and learns the answer from the error message, and the transcript at GET /o/{org}/sessions/{id}/messages keeps that output. The version it pins in the pull request is one it has run.

The rewrite is also long, repetitive and easy to do badly by hand: the fastest conversion keeps the class selectors and proves nothing new. A run has the time to read each component, notice props that were never used and requests that were never mocked, and write assertions about behaviour. It does that while the developer works on something else, and returns with a suite that no longer needs the network to pass.

Frequently asked questions

Why not keep Enzyme and upgrade the adapter?

Enzyme needs an adapter for each React major, and the last official one is enzyme-adapter-react-16. Staying on Enzyme therefore keeps the application on React 16 or on an unofficial adapter. Moving the tests first removes that constraint, and the React upgrade becomes a separate, smaller pull request.

Is there an equivalent of shallow rendering?

No, and that is deliberate. React Testing Library renders the full tree into jsdom and queries the DOM. A test that used shallow to avoid rendering a child usually wanted to avoid that child's side effects, which is solved by mocking the module that causes them, as the agent does for the HTTP client here.

Which version of @testing-library/react works with React 16 or 17?

Version 12. Its peer range is react <18.0.0, while later majors require React 18 or newer and npm refuses to install them beside an older React. The agent reads the locked React version and chooses accordingly, so the same request picks the current release on a React 18 or 19 project.

What about tests that call setState or instance()?

Those reach into a component's internals, which Testing Library does not expose. Each one is rewritten to cause the state change the way a user would, by clicking or typing, and to assert on what is rendered afterwards. Where a test cannot be expressed that way, the agent lists it as not migrated and explains what it was checking.

Will the test count stay the same?

Usually not, and the pull request explains the difference. Tests that only proved a component mounts are merged into tests with assertions, and paths that had no test get one. The description's table accounts for every old test by name so the change in count is traceable.

More jobs like this: Hand off a long task

Keep reading