SYSTEM Cited by 1 source
enzyme-to-rtl-codemod¶
What it is¶
Slack's open-sourced AI-powered codemod that converts Enzyme
test code to React Testing Library (RTL)
test code. Packaged on npm as
@slack/enzyme-to-rtl-codemod
(open-sourced October 2024 in response to external developer demand).
It implements the AST + LLM hybrid conversion pattern: a rule-based AST pass handles deterministic cases and writes in-code annotation comments flagging harder cases; an LLM (Anthropic Claude 2.1 at the time of the original post) consumes the annotated file plus per-test-case DOM context plus a structured prompt to finish the conversion.
Pipeline shape¶
- AST codemod transforms well-understood Enzyme patterns
(top-10 methods:
find,prop,simulate,text,update,instance,props,hostNodes,exists,first— plus Jest-matcher rewrites). For patterns it cannot handle, it leaves in-code annotation comments with suggestions and doc links. - DOM collection runs the Enzyme tests after patching
enzyme.mount/enzyme.shallowto append each test case's rendered HTML (viawrapper.html()) to a file keyed bycurrentTestName(captured fromexpect.getState()inbeforeEach). See concepts/dom-context-injection-for-llm. - LLM conversion sends a three-part structured prompt + the
original test code (in
<code></code>tags) + AST-partial conversion (in<codemod></codemod>tags) + per-test-case DOM tree (in<component><test_case_title>...</test_case_title> and <dom_tree>...</dom_tree></component>tags). - Output is bucketed by pass-rate: fully-converted / partially 50-99% passing / partially 20-49% / partially <20%. Humans verify before merge.
Prompt structure¶
The post discloses the prompt in full, structured in three parts:
- Context setting — names the three input envelopes and
their tags (
<code></code>,<codemod></codemod>,<component>). - Main request — 10 explicit required tasks (complete the
conversion inside
<codemod>, preserve test count, swap Enzyme methods for RTL equivalents, update imports, adjust Jest matchers, return the full file in<code>tags, preserve non-test code and imports verbatim, preservedescribe/itnaming, wrap component rendering in<Provider store={createTestStore()}>) + 7 optional instructions (data-qa→screen.getByTestIdmapping, augmented matchers withDOMsuffix,userEventfor interactions, query priority ordergetByRole→getByText→getByTestId,query*only for non-existence, lowercase regex for text matchers,toBeEmptyDOMElementsubstitution). - Self-evaluation — "evaluate your output and make sure
your converted code is between
<code></code>tags. If there are any deviations from the specified conditions, list them explicitly. If the output adheres to all conditions and uses instructions section, you can simply state 'The output meets all specified conditions.'"
Operational envelope¶
- On-demand: 2-5 minutes per file (local iteration friendly).
- CI nightly: hundreds of files per run, output bucketed for developer triage.
- Adoption: ~64% of files being migrated to RTL at Slack passed through this tool.
- Quality on selected files: ~80% auto-converted, 20% manual.
- At-scale pass-rate: ~500 of ~2,300 test cases auto-passing across 338 files (~22% developer time saved, lower-bound).
See sources/2024-06-19-slack-ai-powered-conversion-from-enzyme-to-react-testing-library for the full retrospective.
Why it matters on the wiki¶
- Canonical production instantiation of patterns/ast-plus-llm-hybrid-conversion at 15,000-test scale.
- Reusable artifact (open-source, npm-installable) beyond test-migration: the pipeline shape applies to any deterministic code-transformation task where LLM-alone hits a quality ceiling.
- Documented prompt structure is itself a reference implementation of the three-part (context / mandatory + optional instructions / self-evaluate) prompt template for code-transformation tasks.
Source¶
- Primary: sources/2024-06-19-slack-ai-powered-conversion-from-enzyme-to-react-testing-library
- npm:
@slack/enzyme-to-rtl-codemod
Related¶
- systems/enzyme — source test framework
- systems/react-testing-library — target test framework
- systems/claude-2-1 — LLM used in the original pipeline
- systems/jest — underlying test runner
- concepts/abstract-syntax-tree — the AST pre-pass primitive
- concepts/dom-context-injection-for-llm — how per-test DOM is captured
- concepts/llm-conversion-hallucination-control — the structural problem
- patterns/ast-plus-llm-hybrid-conversion — the architectural pattern
- patterns/in-code-annotation-as-llm-guidance — how the AST pass shapes LLM output
- companies/slack