# Framework Patterns for `data-testid` Copy-paste patterns for placing valid, unique, stable `data-testid` attributes. Follow the naming and stability rules in [data-testid-conventions.md](data-testid-conventions.md). --- ## React / JSX ```jsx // Semantic markup FIRST (getByRole works), test IDs for durable handles. function LoginForm({ onSubmit }) { return (

); } ``` Lists — stable entity key, never the index: ```jsx function UserTable({ users }) { return ( {users.map((user) => ( ))}
{user.name}
); } ``` Toggle swatch — a native ` ``` > Use `[attr.data-testid]="..."` for dynamic values (Angular needs `attr.` to bind to a real DOM attribute). Static values can stay as plain `data-testid="..."`. Key `@for` with `track` on the same entity id (on Angular 16 and older, `*ngFor` with a `trackBy` on that id). --- ## Svelte 5 ```svelte
``` --- ## Centralizing test IDs Share one source of truth between the app and the test suite so renames are a single edit and typos are caught by the compiler. ```ts // testIds.ts (imported by both components and tests) export const testIds = { loginForm: { root: 'login-form', email: 'login-form-email-input', password: 'login-form-password-input', submit: 'login-form-submit-button', }, userRow: (id: string | number) => `user-row-${id}`, userRowDelete: (id: string | number) => `user-row-${id}-delete-button`, } as const; ``` ```jsx import { testIds } from './testIds'; {users.map((u) => ( ))} ``` ```ts // In the test — same constants, no magic strings. import { testIds } from '../src/testIds'; await page.getByTestId(testIds.loginForm.submit).click(); await page.getByTestId(testIds.userRowDelete(42)).click(); ``` --- ## Production stripping (optional) Only if your team decides test IDs shouldn't ship to real production. **Keep them in dev, CI, and staging** — those are the environments tests and agents run against. React (Babel) — `.babelrc` for production builds only: ```json { "env": { "production": { "plugins": [ ["react-remove-properties", { "properties": ["data-testid"] }] ] } } } ``` Next.js — `next.config.js`: ```js module.exports = { compiler: { // Strips in production builds; leaves dev/test untouched. reactRemoveProperties: process.env.NODE_ENV === 'production', }, }; ``` Vue/Vite — a template compiler node transform, enabled only in production mode: ```js // vite.config.js import { defineConfig } from 'vite'; import vue from '@vitejs/plugin-vue'; const ELEMENT = 1, ATTRIBUTE = 6, DIRECTIVE = 7; // @vue/compiler-core NodeTypes function stripTestIds(node) { if (node.type !== ELEMENT) return; node.props = node.props.filter((p) => !(p.type === ATTRIBUTE && p.name === 'data-testid') && !(p.type === DIRECTIVE && p.name === 'bind' && p.arg?.content === 'data-testid')); } export default defineConfig(({ mode }) => ({ plugins: [ vue({ template: { compilerOptions: { nodeTransforms: mode === 'production' ? [stripTestIds] : [] } } }), ], })); ``` Verify your staging/CI build does **not** strip, or your suite will lose every locator. > Decision guide: stripping reduces payload and hides internals, but adds build complexity and a foot-gun (stripping the wrong env breaks all tests). Many teams simply ship `data-testid` — it's a few bytes and harmless. Strip only when there's a concrete reason.