/testid-instrumentation:testid-instrumentationfrom testid-instrumentation
Add stable `data-testid` attributes to UI code so e2e tests get selectors that survive refactors AND an inspected DOM node greps straight back to the component that rendered it — one attribute, both jobs. Use whenever the user asks to "add data-testid", "make this testable / e2e-ready", "add test selectors", "instrument this page for Playwright / Cypress / Testing Library", or is about to write e2e tests against a screen with no reliable selectors. Use it just as much for the debugging half, even when tests are never mentioned: "which component renders this element?", "I inspected the DOM and can't find it in the code", "make components findable from the browser", or a pasted HTML snippet someone wants traced back to source. Detects the project's existing attribute name and naming convention first and follows it — it never imposes one. Framework-agnostic (React/JSX, Vue, Angular, Svelte, Solid, web components, server-side templates).
Ships in testid-instrumentation. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
data-testid, two jobsA single test-id attribute serves both purposes, so never introduce a second attribute for component identity. One value, two readers:
rg it, land on the source.Identity comes for free from the value, not from a second attribute: any test id that appears literally in source is already a one-grep route back to the file that rendered it. That is the whole trick — and it only holds while the five rules below hold.
Everything else in this skill exists to serve these. Break one and the identity half silently stops working — tests keep passing, so nothing warns you.
data-testid={id} or
data-testid={`${prefix}-${kind}`} is invisible to grep. Dynamic ids keep a static
literal prefix: data-testid={`user-row-${user.id}`} — rg 'user-row-' still finds it.payment-summary-submit ⇄ PaymentSummary. This buys the reverse mapping even when
the tail of the id is interpolated, and it makes rule 2 almost automatic.data-testid="payment-summary"). Tests may never use it; it is what makes
an arbitrary DOM node traceable to a file. This is the one id that is added for the
identity job alone, so keep it to components that matter (see Phase 1). A reused
component's anchor appears many times in the DOM and still exactly once in source — that
satisfies rule 2, which is about the source, and it is why an anchor is a poor thing
for a test to select on its own. Tests scope through it (within(...)), they do not
assume it is alone.Instrument sparingly anyway. Every added attribute is API surface someone must not
rename. An id on every div is noise that makes the real ones invisible — and it breaks
rule 2 faster than anything else.
An open-ended "add test ids everywhere" is not a scope. Survey first (Phase 0), then propose a bounded slice — one feature, one route, the components a named flow touches — and confirm it. A 300-file attribute sweep is unreviewable, and unreviewed ids are how rule 2 gets broken at scale.
Run these before editing anything. What the repo already does wins over every default in this file, including the naming shape in Phase 2 — a literal id is greppable whatever its grammar, so never renumber an existing scheme to match this document.
scripts/testid-scan.sh runs the surveys below and the checks in Phase 4. Call it by
absolute path (<skill-dir>/scripts/testid-scan.sh, last argument = the path to scan)
while your working directory stays in the target project. Use it rather than retyping the
ripgrep: a mangled quote reports "no convention here", which is the worst wrong answer
this phase can produce.
Which attribute? The default is data-testid, but many repos use data-test,
data-cy, data-qa, data-automation-id. Count actual usage:
scripts/testid-scan.sh census .
What do the test tools expect? These must agree with the attribute you add:
use.testIdAttribute in playwright.config.* (default data-testid;
a comma-separated list matches several).configure({ testIdAttribute: '...' }) in the test setup file.@testing-library/cypress; read the existing
selector helper rather than guessing.What is the existing naming shape? Sample real values and copy their grammar (separator, casing, namespaced by feature or by component):
scripts/testid-scan.sh values data-testid . | head -50
Also check how many are already dynamic — rg -n 'data-testid={' finds the
interpolated ones. Those are the existing blind spots for the identity job, worth
reporting even if you do not fix them.
Is there a lint rule or a per-repo doc? eslint plugins that require or forbid the
attribute, a .claude/-level convention file, CONTRIBUTING.md, a testing README.
Follow it verbatim.
Is the attribute stripped in production? Look for compiler.reactRemoveProperties
(Next.js — default regex ^data-test), babel-plugin-react-remove-properties, or a
bundler equivalent. This is the collision point of the two jobs: stripping is right
for test selectors and fatal for debugging a production DOM. If it is on, say so plainly
— identity then only works in dev/staging builds, and the choice (keep stripping, drop
it, or strip only a sub-prefix) belongs to the user, not to you.
See references/frameworks.md.
Which framework and how do attributes reach the DOM? Passing an attribute to a component is not the same as putting it on a DOM node — read references/frameworks.md before touching JSX/SFC/templates.
If the project has no convention at all, state that you are introducing one, use the defaults below, and keep it consistent across the whole change.
Two lists, because the two jobs want different things.
For tests — test ids are the fallback, not the first choice. Prefer selectors that assert something a user perceives (role + accessible name, label text) because they break when the UX breaks. Add a test id when:
div acting as a control)..css-1x2y3z, nth-child, deep
descendant chains). Replacing those is the highest-value work here.For identity — one root anchor per component that someone would plausibly need to find:
screens, routes, feature sections, cards, rows, modals, anything with real logic inside.
Skip it for leaf primitives that render hundreds of times (Button, Icon, Text,
layout/spacing wrappers) unless the user asks — the DOM weight is real and the answer there
is usually "it's the design-system Button", which nobody needed an attribute to learn.
Do not add ids to decorative nodes, elements already reachable by a stable role+name that no test needs to scope into, or anything under a generated/vendored directory.
Before adding, check whether the element is already reachable and already traceable. A skipped addition with a stated reason beats a redundant attribute.
Follow the repo's grammar if one exists. Otherwise default to lowercase kebab-case, prefixed with the component's own name:
<component-kebab>[-<element>][-<qualifier>]
payment-summary # root anchor → PaymentSummary
payment-summary-submit # the button → PaymentSummary
user-table-row-<userId> # list row → UserTableRow
settings-profile-avatar-upload # nested → SettingsProfile
The mapping value ⇄ symbol must be mechanical in both directions: kebab-case the
component symbol to get the prefix, PascalCase the prefix to get the symbol back. Do not
abbreviate one side (pmt-summary-submit costs you the reverse lookup for nothing).
Then:
-row-3 breaks on sort/filter/pagination. Use a stable
business key: user-table-row-${user.id}. With no stable key, put the id on the container
and let the test scope by visible content inside it.A full before/after on one card — which nodes earn an id, which stay bare, and what the
diff looks like — is in references/worked-example.md.
'data-testid'?: string) rather than widening the props type to any or spreading
unknown rest props into the DOM.id, name, role) so diffs read cleanly.The grep round-trip — this is the definition of done for the identity job. For every id you added, run the lookup a developer would run from devtools and confirm it lands on your file, once:
scripts/testid-scan.sh find payment-summary-submit .
(It retries on the static prefix automatically, which is what a dynamic id needs.) Zero hits means you broke rule 1; several unrelated hits mean you broke rule 2. Fix the value, not the grep.
Nothing existing was renamed. For every id you touched, rg the whole repo —
including tests, page objects, selector constant files, and analytics/tracking code that
may key off the same attribute.
Unintended duplicates:
scripts/testid-scan.sh dupes data-testid .
Every survivor must be an intentional shared template. Explain each one you keep.
The attribute actually lands in the DOM. Static edits are not proof — a component that drops unknown props renders nothing. Confirm by rendering: a component/e2e test, a dev server plus a DOM query, or a snapshot. If you could not render it, say unverified and name what you skipped.
The project's own checks — typecheck, lint, and the test suite for the touched area, once at the end. Report real output, not an assumption.
Keep it to this shape. The reader's two questions are "what is now selectable?" and "what did you decide on my behalf?" — the skipped list and the caveats answer the second, so do not drop them just because they are the boring half.
**Convention**: <attribute> + <naming grammar>, found in <where> / newly introduced.
**Added** (n): <id> → <file:line>, one line each, grouped by component.
**Root anchors**: <components that got one>.
**Skipped**: <element or component> — <why: already reachable, leaf primitive, generated code>.
**Untouched**: <existing ids left alone, and which tests depend on them>.
**Verified**: grep round-trip <pass/fail>, duplicates <none/list>, DOM observed via <how> or
**unverified — <what you could not run>**.
**Caveats**: production stripping <on/off and what that costs>, passthrough you had to add,
anything the team must keep doing.
Given a DOM snippet from devtools:
rg -n 'payment-summary-submit'. Instrumented code ends here.rg -n 'user-table-row-' finds `user-table-row-${user.id}`).payment-summary →
rg -n 'PaymentSummary', filtered to its definition/export.aria-label. Generated hash classes (css-1x2y3z) are dead ends — do not burn time.Framework devtools (React/Vue/Svelte extensions) map an element to a component tree in dev builds; use them when available. The point of rule 1 is that grep still works where they are not — a production build, a bug report screenshot, a colleague's pasted HTML.
keys.What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub thedv91/coding-skills --plugin testid-instrumentationOther plugins' skills for the same job